• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method overriding

 
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a code:

subClass obj=new superClass();
obj.eat();
Here eat() method is overridden in the sub class.

as we know that in case of method overriding Object type(not reference type determine) ,determine which overridden method is used at run time.so in this case this code should work according to statement.
But this show compilation error for incompatible type.
so i want to know ,why?
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can assign a subtype object to a supertype variable:

superClass obj = new subClass();

But you cannot assign a supertype object to a subtype variable:

subClass obj=new superClass();

John
 
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gursewak Singh wrote:i have a code:

subClass obj=new superClass();
obj.eat();
Here eat() method is overridden in the sub class.

as we know that in case of method overriding Object type(not reference type determine) ,determine which overridden method is used at run time.so in this case this code should work according to statement.
But this show compilation error for incompatible type.
so i want to know ,why?



To make it a little easier to spot consider this code:



....and consider this code:



Explanations are in the comments. let me know if you have more questions.
 
Gursewak Singh
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks John and Boris Mechkov

i got it.......
thank you so much
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gursewak Singh wrote:thanks John and Boris Mechkov

i got it.......
thank you so much


That's what he explained is correct see this link http://java-intellectual.blogspot.com/2011/12/java-method-overriding-rules.html
 
There's a way to do it better - find it. -Edison. A better tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic