2

Hi i have a specific question for inheritance in Java. following is my code

class Parent{
    int x = 5;
    public void method(){
        System.out.println("Parent"+ x);
    }
}
public class Child extends Parent{
    int x = 4;
    public void method(){
        System.out.println("Child"+ x);
    }
    public static void main(String[] args){
            Parent p = new Child();
            System.out.println(((Child) p).x);
            System.out.println(p.x);
        }
}

Now my question is what happens actually behind the scene while running this program.

  • what gets inherited?
  • where in the memory location?
  • why first syso gives 4 and second gives 5?(this i am able to understand at some extent but clarification on above two will help understanding it more clearly)

please guide

dev2d
  • 4,245
  • 3
  • 31
  • 54

5 Answers5

1

In Java there is no variable overriding and only method overriding.

    System.out.println(((Child) p).x);

That line telling point p to the Child and get that x variable.

The line, System.out.println(p.x); telling that print Parent's x

Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • yes i understood that but how exactly it gets referenced?? i mean it might get stored somewhere in memory right? – dev2d Nov 27 '13 at 13:15
  • @VJD Can you be more clear ? All the object's stores in heap only. didn't get your question. – Suresh Atta Nov 27 '13 at 13:18
0

It is all about Java inheritance and overriding. method() in the parent class will override by child's method.

System.out.println(((Child) p).x);// here you are invoking child 

Then you will get child's attributes.

And next one is all about Java polymorphism.

 ((Child) p).x // invoking object type, x=4 (object is child ) 

  p.x  // invoking reference type, x=5 (reference is parent)

Refer this links and this one.

Community
  • 1
  • 1
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

what gets inherited?

All the public and protected methods are inherited in the subclass. Fields are never inherited.

where in the memory location?

I've written a blog post about Object creation process in Java. I think you will understand from it better.

why first syso gives 4 and second gives 5?

Field access is always resolved at based on the declared type of the reference, and not the actual object type. So, p.x will access the field of Parent, as declared type of p is Parent. Whereas, ((Child)p).x will access the field of Child, as you have type casted the reference p to Child, and now the declared type considered is Child.

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Reason for `p.x` prints `5` and `((Child) p).x` prints `4` because fields are not polymorphic therefore those are printed according to reference type – Nandkumar Tekale Nov 27 '13 at 13:18
  • @Rohit Jain, i have gone through your blog, its very good. now there remains only 1 doubt, can you please help me explaining how and where in memory will things related to Parent and Child will get stored? – dev2d Nov 27 '13 at 15:43
0

In Java, Always sub class object will be instantiated. Super class constructor is called while instantiating child class only to declare super class variables and allocate memory in sub class object. And By default super class public and protected methods are inherited to the sub class. So concluding this, Memory is allocated only for sub class. You can verify it in local, Using JDK Java Virtual VM to see instance list.

0

For instance variables in inheritance: Assume two circles in memory

Inner circle is parent with parent instance variables in it and outer circle is child having child instance variables in it.

Child ref can access parent instance variables but vice versa is not true because parent instances do not know about child instances
https://i.stack.imgur.com/sR1bS.jpg
https://www.java-forums.org/new-java/96742-how-memory-allocated-during-inheritance.html

class Test1 {       
    public int gear = 10; 
    public int speed =110 ;
} 

// derived class 
class MountainBike extends Test1 
{ 
    //public int gear = 9; 
    public int speed =11 ; 
    // the MountainBike subclass adds one more field 
    //public int seatHeight;
} 

// driver class 
public class Test 
{ 
    public static void main(String args[]) 
    {           
        Test1 mb = new MountainBike(); 
        System.out.println("Hii"+mb.gear); 

        MountainBike m = new MountainBike();

        System.out.println("Hii123 "+m.gear);
    }
} 

The above code will print 10..10 as a output.

Nawed Nabi Zada
  • 2,819
  • 5
  • 29
  • 40