wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in polymorphism" Watch "doubt in polymorphism" New topic
Author

doubt in polymorphism

rosal saai
Greenhorn

Joined: Sep 20, 2008
Posts: 3
in polymorphism, consider dog class extends from animal class

Animal a= new Dog();

and when a.play() is called, whether it will call animal class mehod or dog's method? it depends on reference variable type?
grigoris philippoussis
Greenhorn

Joined: Oct 22, 2007
Posts: 16
if you've overridden play() in Dog, it will call that. otherwise it will call the base class method.
the compiler looks at the reference type, at run-time, it looks at the object.
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

Moreover,


With animal reference to dog you can only call methods which are defined in animal class only, you can not call dog's methods with animal reference to dog. You can call dogs methods with dog reference
I mean

the play() method must be defined in animal, otherwise if it were in dog class you cannot say

animalReferencetoDog.play() <----it must be defined in animal class


[ October 24, 2008: Message edited by: Anut Walidera ]

blog: http://tunatore.wordpress.com
SCJP 6.0 + SCWCD 1.5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

Rosal, why don't you write a small program to find out the answer?

Writing small programs and experimenting yourself is a great way to learn - much better than just learning theoretical answers by heart.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Remember that play must be a instance method. If it is static you will call play in Animal:





"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
krishna pr
Greenhorn

Joined: Oct 28, 2008
Posts: 8
Ciao Anut,


With animal reference to dog you can only call methods which are defined in animal class only, you can not call dog's methods with animal reference to dog.

I agree with you, but what if the method is overridden in the Dog class, Can't the animal reference be used then to call the overridden method in the Dog class.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

No that is not posible.



But why not write a program yourself to find out? It's easier, faster and you learn much more from it.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
With animal reference to dog you can only call methods which are defined in animal class only, you can not call dog's methods with animal reference to dog.

I agree with you, but what if the method is overridden in the Dog class, Can't the animal reference be used then to call the overridden method in the Dog class.


If the method is overriden in the Dog class, that implies that the method is declared in the Animal class. Or else you would not be overriding the method. Yes, the Animal reference can be used to call the overridden method in the Dog class. The Animal reference cannot be used to call a method that is only declared and implemented in the Dog class.

With an Animal reference, you can only call methods that are declared in the Animal class. To understand this better, if the Dog class has any methods that are not declared in the Animal class, you will not be able to call these methods with an Animal reference, even if the actual object is an instance of Dog.
[ October 28, 2008: Message edited by: James Clark ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in polymorphism
 
Similar Threads
class cating
Upcasting & Downcasting
it's polymorphism or a upcasting
Polymorphism
JAVA Upcasting and Downcasting of reference variables