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