Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can we execute the Overrided method of Base class with reference of type Super class?

 
Tej Kumar Ch
Greenhorn
Posts: 15
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

OutPut for the above snippet is :
In B
In B

My doubt is

With the above I am expecting to invoke the Super class method and to print "A", But not happening.Its always calling the polymorphic method in B only.
Is there way to invoke that?

I tried with Casting but in vain

With Respect
Tej

 
Henry Wong
author
Marshal
Pie
Posts: 22108
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tej Kumar Ch wrote:
With the above I am expecting to invoke the Super class method and to print "A", But not happening.Its always calling the polymorphic method in B only.
Is there way to invoke that?


With overridden methods, only the subclass can call the super class' version of the method -- and even then, the subclass can only call the super class version that was directly overridden.

Henry
 
Tej Kumar Ch
Greenhorn
Posts: 15
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With overridden methods, only the subclass can call the super class' version of the method -- and even then, the subclass can only call the super class version that was directly overridden.

Hi Henry,

I am afraid , I didnt get your point, can you please elaborate.

With Respect
Tej
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tej Kumar Ch wrote:With the above I am expecting to invoke the Super class method and to print "A", But not happening.Its always calling the polymorphic method in B only.
Is there way to invoke that?

Your understanding is wrong!

You have to remember 1 very important rule: Which instance methods you can call/invoke is determined at compile time based on the reference variable type. Which instance method is actually executed is decided at runtime based on the type of the actual object (= polymorphism). That's really a very, very important rule!

Now we go back to your example:So if we apply the aforementioned rule to this code:
1/ the type of reference variable x is A => all methods defined in class A can be invoked
2/ the type of the actual object reference variable x is referring to, is B => the method mA in B (which overrides method mA in A) will be executed (printing In Mb).
3/ the type of reference variable x is A, the code on line1 doesn't make any difference and is completely equivalent to the code on line2. Because you cast, you are temporarely changing the type of the reference variable to A (but it was already A, so it doesn't make any difference). You do not change the type of the actual object the reference variable is referring to (which is required to execute another method)

So which options do you have to execute method mA in A?
1/ Make sure the actual object the reference variable is referring to is of type A
2/ In the overridden method you can invoke the base class version of the method using the super keyword. So change the code of method mA in B toNow the codeprints
A
In Mb


Hope it helps!
Kind regards,
Roel
 
Tej Kumar Ch
Greenhorn
Posts: 15
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

Thanks a lot for your elaborate and explanatory responses, it cleared my query

With Respect
Tej
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic