Polymorphism problem in java..
-
When we assign a subclass object to its superclass reference then the superclass reference can only access the methods that are in the superclass, it can't even the aceess its overridden methods in subclass. if the above lines are true then how the following code can works ... class a { int x; void show() { System.out.println(x); } } class b extends a { void show() { System.out.println("in subclass"); } void show2() { System.out.println("in show2"); } } class Main { public static void main(String args[]) { b bRef=new b(); a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass? aRef.show(); bRef.show2(); } }
-
When we assign a subclass object to its superclass reference then the superclass reference can only access the methods that are in the superclass, it can't even the aceess its overridden methods in subclass. if the above lines are true then how the following code can works ... class a { int x; void show() { System.out.println(x); } } class b extends a { void show() { System.out.println("in subclass"); } void show2() { System.out.println("in show2"); } } class Main { public static void main(String args[]) { b bRef=new b(); a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass? aRef.show(); bRef.show2(); } }
-
When we assign a subclass object to its superclass reference then the superclass reference can only access the methods that are in the superclass, it can't even the aceess its overridden methods in subclass. if the above lines are true then how the following code can works ... class a { int x; void show() { System.out.println(x); } } class b extends a { void show() { System.out.println("in subclass"); } void show2() { System.out.println("in show2"); } } class Main { public static void main(String args[]) { b bRef=new b(); a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass? aRef.show(); bRef.show2(); } }
-
When we assign a subclass object to its superclass reference then the superclass reference can only access the methods that are in the superclass, it can't even the aceess its overridden methods in subclass. if the above lines are true then how the following code can works ... class a { int x; void show() { System.out.println(x); } } class b extends a { void show() { System.out.println("in subclass"); } void show2() { System.out.println("in show2"); } } class Main { public static void main(String args[]) { b bRef=new b(); a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass? aRef.show(); bRef.show2(); } }
gateway23 wrote:
a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass?
It is because eventhough you have declared aRef to be of class 'a', it acutally holds an instance of class 'b' at runtime. That is what Polymorphism is all about. That's not a problem, that's the expected behavior.
-
When we assign a subclass object to its superclass reference then the superclass reference can only access the methods that are in the superclass, it can't even the aceess its overridden methods in subclass. if the above lines are true then how the following code can works ... class a { int x; void show() { System.out.println(x); } } class b extends a { void show() { System.out.println("in subclass"); } void show2() { System.out.println("in show2"); } } class Main { public static void main(String args[]) { b bRef=new b(); a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass? aRef.show(); bRef.show2(); } }
Hello friend... Just add this line in main()
System.out.println("aRef's type is " + aRef.getClass().toString());
and output for this line must me :aRef's type is b
when you call aRef.show() then first this call must be resolved by class of aRef's object if fuction show() not found then jvm try to resolved it from super class. -
gateway23 wrote:
a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass?
It is because eventhough you have declared aRef to be of class 'a', it acutally holds an instance of class 'b' at runtime. That is what Polymorphism is all about. That's not a problem, that's the expected behavior.
-
Shameel wrote:
Don't read the word "holds" literally
That is not the point. If you must give a technical answer then make sure it is accurate and clear. Remember, many of these questions are asked by people who do not have a lot of background knowledge or experience and rely on these answers to help them in their learning. The difference between "holds" and "points to" may be crucial to a new developer.
The best things in life are not things.
-
Shameel wrote:
Don't read the word "holds" literally
That is not the point. If you must give a technical answer then make sure it is accurate and clear. Remember, many of these questions are asked by people who do not have a lot of background knowledge or experience and rely on these answers to help them in their learning. The difference between "holds" and "points to" may be crucial to a new developer.
The best things in life are not things.