Forum Stats

  • 3,840,145 Users
  • 2,262,571 Discussions
  • 7,901,155 Comments

Discussions

Inheritance quesiton

Prem
Prem Member Posts: 88
edited Nov 22, 2015 3:14PM in New To Java

Hi, 

I am expecting output from following code as 10 But it gives output of 5. Can anybody tell me why this.id is returning parent class value? My understanding is by default you will have getId() which in thurn returns this.id.So when Ihave created  Child c=new Child() , it should look into child class to access that method and should return value of child class itself but it is returning parent class value. I m too much confused. Kindly guide me.

[code]

Class Parent{

private int id=5;

public int getId(){

  return this.id;

}

}

Class child extends Parent{

private int id=10;

public static void main(string []ar){

Child c = new Child();

System.out.println(c.getId());

}

}

[/code]

Answers

  • Unknown
    edited Oct 10, 2015 12:46PM
    I am expecting output from following code as 10 But it gives output of 5. Can anybody tell me why this.id is returning parent class value? My understanding is by default you will have getId() which in thurn returns this.id.So when Ihave created  Child c=new Child() , it should look into child class to access that method and should return value of child class itself but it is returning parent class value. I m too much confused. Kindly guide me.
    
    [code]
    Class Parent{
    private int id=5;
    
    public int getId(){
      return this.id;
    }
    }
    
    Class child extends Parent{
    private int id=10;
    
    public static void main(string []ar){
    Child c = new Child();
    System.out.println(c.getId());
    
    }
    
    }
       [/code]
    
    

    Correct - the output is 5. Why do you think it should be 10?

    You made the parent variable 'private' - that means it will NOT be inherited by extending classes.

    You also created a public 'getId' method in the parent that returns that private variable..So that method returns the uninherited value.

    See The Java Tutorials trails on Inheritance.

    https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

    Private Members in a Superclass

    A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
    
  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Oct 10, 2015 2:32PM
    Prem wrote:
    I am expecting output from following code as 10 But it gives output of 5. Can anybody tell me why this.id is returning parent class value? My understanding is by default you will have getId() which in thurn returns this.id.So when Ihave created  Child c=new Child() , it should look into child class to access that method and should return value of child class itself but it is returning parent class value.
    
    

    Your understanding is wrong.

    Any parent class never knows about anything of its child classes.

    the only weay to access information of child class from the parent class is by using abstract methods in the parent which are guaranteed to be implemented in the child classes:

    class Parent{
    private int id=5;
    
    public int getId(){
      return getChildsId();
    }
    protected abstract int getChildsId();
    }
    
    class Child extends Parent{
    private int id=10;
    
    @Override
    protected int getChildsId(){
      return id;
    }
    
    public static void main(string []ar){
    Child c = new Child();
    System.out.println(c.getId());
    }
    }
    

    this returns the childs ID (10)

    bye

    TPD

  • user10137999
    user10137999 Member Posts: 54
    edited Nov 22, 2015 1:02AM

    How can a non Abstract class contains an abstract method? I guess you forgot to mention Parent class as Abstract. Otherwise this code will produce compile time exception

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Nov 22, 2015 6:23AM
    user10137999 wrote:
    
    How can a non Abstract class contains an abstract method? I guess you forgot to mention Parent class as Abstract. Otherwise this code will produce compile time exception
    

    This is correct, a class that contains abstract methods must be abstract itself.

    Also it cannot be instantiated (directly).

    Thank you for pointing to this.

    But as per my understanding this fact does not contribute to the OPs question.

    bye

    TPD

  • Unknown
    edited Nov 22, 2015 3:14PM
    But as per my understanding this fact does not contribute to the OPs question.
    

    Huh? You posted INVALID code. How does that fact 'contribute to the OPs question'?

This discussion has been closed.