Win a copy of Spring Boot in Practice this week in the Spring 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic