Results 1 to 5 of 5
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default A question on Inheritance and fields

    Hi people. Here's a question on Inheritance:

    I have a Human Class:

    Java Code:
    public class Human 
    {
    	private String name;
    	
    	public String getName() 
    	{
    		return name;
    	}
    
    	public void setName(String name) 
    	{
    		this.name = name;
    	}
    }
    and I have my Student class which extends the Human class:

    Java Code:
    public class Student extends Human 
    {
    	private String name;
    	
    	public void setName(String a) 
    	{
    		this.name=a+a;
    	}
    }
    and my Test Class:

    Java Code:
    public class Test 
    {
    	public static void main(String[] args) 
    	{
    		Student student1 = new Student();
    		student1.setName("John");
    		System.out.println(student1.getName());
    	}
    }
    I am expecting that student1 gets the name: JohnJohn. However here is the concole output: null.

    Can anyone explain please?

    Thank you.

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: A question on Inheritance and fields

    Answering my own question:

    If I override the getName() method, I get JohnJohn...

    Thanks.

  3. #3
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: A question on Inheritance and fields

    Quote Originally Posted by fatabass View Post
    Answering my own question:

    If I override the getName() method, I get JohnJohn...

    Thanks.
    You should make name protected (or default) in Human and remove it in Student. Then you don't have to override getName().

    You shouldn't normally repeat fields from the superclass in subclasses. The subclass automatically inherits fields of the superclass.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: A question on Inheritance and fields

    Quote Originally Posted by 2by4 View Post
    ...The subclass automatically inherits fields of the superclass.
    Agree ... except for the private fields -- which still may be accessible via accessor and mutator methods though.

  5. #5
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: A question on Inheritance and fields

    Quote Originally Posted by Fubarable View Post
    Agree ... except for the private fields -- which still may be accessible via accessor and mutator methods though.
    Important point, although I would say they are inherited but the compiler limits their access.

    Reflection for example can get at them.

Similar Threads

  1. Inheritance Program Question
    By simon.black325 in forum New To Java
    Replies: 11
    Last Post: 09-08-2011, 03:01 AM
  2. inheritance question
    By Jaevko in forum New To Java
    Replies: 7
    Last Post: 08-31-2011, 07:30 AM
  3. A question about inheritance / polymorphism
    By gabriels in forum New To Java
    Replies: 2
    Last Post: 02-26-2011, 11:53 AM
  4. Question about inheritance
    By hei1233212000 in forum New To Java
    Replies: 3
    Last Post: 11-12-2010, 11:38 AM
  5. a question about inheritance
    By litojs in forum New To Java
    Replies: 8
    Last Post: 11-10-2008, 10:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •