• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why superclass reference cant invoke subclass nonoverriden method

 
Debasis behera
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why a superclass reference to subclass object only invoke overriden and superclass method but it cant invoke subclass method
WHY WHY
 
Debasis behera
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me quickly
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Animal, Dog and Cat example.
The compiler only knows that what you are referencing IS-A Animal.
So it can only calls the methods defined in the Animal class.
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Debasis behera wrote:please help me quickly

Debasis,
Welcome to the Ranch.

Please read http://faq.javaranch.com/java/PatienceIsAVirtue and http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 243
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leandro Coutinho wrote: The Animal, Dog and Cat example.

Classic stuff. See the below code.



All animals can call eat() method , but it doesn't make sense to call bark() or meow() on an animal.

Thanks,
Srikkanth
 
Debasis behera
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if superclass reference refers to subclass object then why we cant invoke subclass method,the actual object is subclass so why
if i write
Animal a=new Dog();//noerror
a.bark()//error why

the actual object is of dog,so why it shows that method not found
 
Debasis behera
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Debasis behera wrote:if superclass reference refers to subclass object then why we cant invoke subclass method,the actual object is subclass so why
if i write
Animal a=new Dog();//noerror
a.bark()//error why

the actual object is of dog,so why it shows that method not found


The "a" variable is an Animal variable and it only allows you to call Animal methods.

This is as it should be. The trick here is to make Animal a good abstraction for the intended subclasses. In this particular case you could introduce a method called makeSound in Animal. Then a Dog will implement this by barking and a Cat will say miaow, etcetera. This is called polymorphic behavior.



This idea that the superclass "covers" for all subclasses and that any sublass object works equally well in code written using superclass variables even has a name. It's called the Liskov substitution principle.

 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Debasis behera wrote:please help me

Maneesh Godbole wrote:Debasis,
Welcome to the Ranch.

Please read http://faq.javaranch.com/java/PatienceIsAVirtue and http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch

Apparently, Debasis finds reading is quite hard to do.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Debasis behera wrote:if superclass reference refers to subclass object then why we cant invoke subclass method,the actual object is subclass so why
if i write
Animal a=new Dog();//noerror
a.bark()//error why

the actual object is of dog,so why it shows that method not found

Because the compiler doesn't know a is a Dog, and in fact, it doesn't have to be. What if you put this line between your two lines of code:
a is still an Animal, but no longer a Dog. As such, it can't bark() anymore.
 
Debasis behera
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remembering that you can cast to call the dog method

if(a instanceof Dog)
((Dog)a).bark();
 
Campbell Ritchie
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you cast a reference type, you are not quite dealing with a simple pointer/reference to the superclass, so that is a bit different from the original question.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leandro Coutinho wrote:remembering that you can cast to call the dog method

if(a instanceof Dog)
((Dog)a).bark();


Instead of remembering this advice I suggest the OP forgets about it for the time being.

There is a time for downcasting but getting into the habit of doing it too early is not good for your development to a good OO programmer.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic