Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Inheritance problems

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Inheritance problems

    Hey, this is my code so far, but how do I set the name in Contact using the variables in Name?

    public class Name extends Contact
    {
    private String fName;
    private String sName;

    public Name()
    {

    }

    public void setFName(String fName)
    {
    this.fName = fName;
    }

    public void setLName(String sName)
    {
    this.sName = sName;
    }
    }



    public class Contact
    {
    private Name fName;
    //private String sName;
    //private String street;
    //private String town;
    // private String postcode;

    public Contact()
    {

    }

    private void contacts(String fName, String sName, String street, String town, String postcode)
    {
    Name.fName = fName;


    }
    }

    Thanks in advance, Tom


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Inheritance problems

    Your problem is your confusion between the variables and the inheritance/classes. The statement private Name fName; in class Contact is a backward reference. if fName is the same for both classes you won't need to declare it in your class Name but define fName as protected (only for inheritance) or public (also from outside) in the "mother" class Contact. Your code won't make any sense and is logically wrong. It's also not a recursive technique.
    public class Contact{
             prrotected String fName; // or public String fName;
             //wrong: private Name fName; 
    ..

Similar Threads

  1. Confusion about inheritance
    By MeteoricDragon in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 17th, 2012, 06:17 PM
  2. Inheritance
    By lewzax in forum Object Oriented Programming
    Replies: 4
    Last Post: July 8th, 2011, 01:51 PM
  3. inheritance help
    By justin3492 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 07:45 PM
  4. inheritance
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2010, 09:23 PM
  5. Problem with OOP - Inheritance
    By connex in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2009, 11:11 PM