Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 15
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

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

With Respect
Tej
 
I am mighty! And this is a mighty small ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic