Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By JosAH

Thread: Inheritance and instance variables.

  1. #1
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Inheritance and instance variables.

    Greetings everyone.

    My little brother(just starting to learn Java) game me this question and frankly i was pretty amused at how different the result of this program was; from what i had expected. Here is the entire code (you can copy-paste this to run it) :

    Java Code:
    package com.inheritance.googly;
    
    class Parent {
    	private String name = "Parent";
    
    	public String getName() {
    		return this.name;// note that we are using "this"
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    }
    
    class Child extends Parent {
    	private String name = "Child";
    }
    
    public class GooglyTest {
    
    	public static void main(String[] args) {
    		Child child = new Child();
    		System.out.println(child.getName());//prints "Parent", Why?
    	}
    
    }
    Now in the above example, the Child class has an instance variable called "name" ( representing it's state) and gets the set/get methods(it's behavior) inherited from it's parent. Notice that the Parent too has a private instance variable called "name"(with a different value). Now if i create a Child instance an call getName(), (which uses "this"), it still returns "Parent".

    Should'nt the get method return the Child's instance variable instead of the Parent's? Should'nt the "this" reference be taking the Child's "name" as the object being run is of type "Child" ?

    Please advice.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default Re: Inheritance and instance variables.

    Only the parent class has a method getName(); it is inherited by the child class but it does what it does: it returns the name field from the parent class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: Inheritance and instance variables.

    Thanks Jos.

    I was wondering that the method getName()'s implementation should take the "name" variable referenced by "this"(as in the code); and the "this" reference is for the Child class, so the child's "name" should be picked up. So in the code "return this.name" should be pointing to the Child's name?

    Please advice.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default Re: Inheritance and instance variables.

    Quote Originally Posted by garyiskidding View Post
    Thanks Jos.

    I was wondering that the method getName()'s implementation should take the "name" variable referenced by "this"(as in the code); and the "this" reference is for the Child class, so the child's "name" should be picked up. So in the code "return this.name" should be pointing to the Child's name?

    Please advice.
    That only works with methods, i.e. they are 'virtual' (in C++ speak) unless final. Think of it: what is that method to do if the type of name in the subclass is different from the type of the name variable in the superclass? And what to do if the type of a name variable in a sub-subclass happens to be the same again as the type of name in the parent class? A superclass knows nothing about its subclasses (if any).

    kind regards,

    Jos
    garyiskidding likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: Inheritance and instance variables.

    Yes, i get it now. The method needs to behave as it should with or without the subclass.

    Thanks again Jos.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default Re: Inheritance and instance variables.

    Quote Originally Posted by garyiskidding View Post
    Yes, i get it now. The method needs to behave as it should with or without the subclass.

    Thanks again Jos.
    You're welcome of course; you can always use the overriding mechanism for methods to do what you intended:

    Java Code:
    class Parent {
        private String name = "Parent";
    
       protected String getMyName() { return name; }
     
        public String getName() { return getMyName(); }
    }
     
    class Child extends Parent {
        private String name = "Child";
    
       protected String getMyName() { return name; }
    }
     
    public class GooglyTest {
     
        public static void main(String[] args) {
            Child child = new Child();
            System.out.println(child.getName());//prints "Child".
        }
     
    }
    The getMyName() method is overridden in the child class, so the getName() method returns the name of the subclass ...

    kind regards,

    Jos
    Last edited by JosAH; 05-21-2013 at 01:05 PM.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Instance variables
    By jwl in forum New To Java
    Replies: 2
    Last Post: 10-24-2012, 11:29 PM
  2. instance variables initialization
    By ghostrider in forum New To Java
    Replies: 6
    Last Post: 08-27-2012, 07:00 AM
  3. Instance Variables
    By TriSBR in forum New To Java
    Replies: 2
    Last Post: 11-16-2011, 04:16 PM
  4. Instance variables
    By lala in forum New To Java
    Replies: 1
    Last Post: 01-26-2011, 04:38 PM
  5. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM

Tags for this Thread

Posting Permissions

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