File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes why superclass reference cant invoke subclass nonoverriden method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why superclass reference cant invoke subclass nonoverriden method" Watch "why superclass reference cant invoke subclass nonoverriden method" New topic
Author

why superclass reference cant invoke subclass nonoverriden method

Debasis behera
Greenhorn

Joined: Oct 26, 2009
Posts: 14
Why a superclass reference to subclass object only invoke overriden and superclass method but it cant invoke subclass method
WHY WHY


java
Debasis behera
Greenhorn

Joined: Oct 26, 2009
Posts: 14
please help me quickly
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
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

Joined: Jul 26, 2007
Posts: 10451
    
    8

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
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

Joined: Oct 26, 2009
Posts: 14
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

Joined: Oct 26, 2009
Posts: 14
please help me
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
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

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

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

Joined: Oct 26, 2009
Posts: 14
thanks
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
remembering that you can cast to call the dog method

if(a instanceof Dog)
((Dog)a).bark();
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Oct 22, 2009
Posts: 237
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why superclass reference cant invoke subclass nonoverriden method