Variables linking the binding of the VS-method to polymorphism

I observe a behavior that when we call a variable from a polymorphic object, then it calls the parent variable, but when we call the method with the same polymorphic object, then it calls the child method. Why is this polymorphism behavior in Java? Why doesn't Java handle polymorphic variables and methods in the same way?

class Parent{ int age =10; public void showAge(){ System.out.println("Parent Age:"+age); } } class ChildOne extends Parent{ int age = 20; public void showAge(){ System.out.println("child one age:"+age); } } class ChildTwo extends Parent{ int age = 30; public void showAge(){ System.out.println("Child Two Age:"+age); } } public class Test{ public static void main(String[] args) { Parent parentChildOne = new ChildOne(); System.out.println("parentChildOne.age: "+parentChildOne.age); parentChildOne.showAge(); Parent parentChildTwo = new ChildTwo(); System.out.println("parentChildTwo.age: "+parentChildTwo.age); parentChildTwo.showAge(); } } 

Here is the result:

 parentChildOne.age: 10 child one age:20 parentChildTwo.age: 10 Child Two Age:30 
+4
source share
4 answers

First of all, keep in mind that Your variables are not polymorphic and the next thing climax is your point

  Parent parentChildOne = new ChildOne(); Parent parentChildTwo = new ChildTwo(); 

Look, when you try to call a method using Parent parentChildOne , then it should call the child method because it is overridden and according to polymorphism it should be called.

Now let's see Parent parentChildOne the same object for variables, now nothing happens with polymorphism here, but jvm now has to do with the concept of shadowing
So thatโ€™s why they both demonstrate their real behavior
Please follow this shading guide in java

+1
source

Variables are not polymorphic in Java.

Instead, instance variables in child classes are shadow instance variables with the same name in the parent class. See Also Can a parent and child class in Java have the same instance variable?

+1
source

parentChildOne and parentChildTwo are of type Parent . So you type age Parent . The same thing happens with the showAge() method, but age obscured by child classes.

0
source

Please view comments,

 class Parent{ int age =10; public void showAge(){ System.out.println("Parent Age:"+age); } } class ChildOne extends Parent{ //when you extends Parent the inherited members are like //and initialized into the default constructor // int super.age =10; int age = 20; public void showAge(){ System.out.println("child one age:"+age); } } class ChildTwo extends Parent{ //when you extends Parent the inherited members are like //and initialized into the default constructor // int super.age =10; int age = 30; public void showAge(){ System.out.println("Child Two Age:"+age); } } public class Test{ public static void main(String[] args) { Parent parentChildOne = new ChildOne(); // when we call like this, goes to the parent type of the variable instead of object. System.out.println("parentChildOne.age: "+parentChildOne.age); parentChildOne.showAge(); Parent parentChildTwo = new ChildTwo(); // when we call like this, goes to the parent type of the variable instead of object. System.out.println("parentChildTwo.age: "+parentChildTwo.age); parentChildTwo.showAge(); } } 
0
source

All Articles