File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes polymorphism, overriding - confusion ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "polymorphism, overriding - confusion ?" Watch "polymorphism, overriding - confusion ?" New topic
Author

polymorphism, overriding - confusion ?

lucas van derstappen
Greenhorn

Joined: Feb 25, 2009
Posts: 6
I am reading now a SCJP5 study guide book (Osborne, 2006 - Sierra,Bates) and i am little confused.

Problem is in these two sentences : ( two minute drill chapter 2, pages 153,154)

Chapter about Polymorphism :
(I.) Reference variable`s type (not the object`s type) determines which methods can be called.

And

Chapter about overriding :
(II.) Object type (not reference variable`s type), determines which overriden method is used at runtime.


Just imagine :
class Animal {
void stinky(){
System.out.println("stinky animal !");
}
}

class Dog extends Animal{
public void stinky() {
System.out.println("stinky dog !");
}

}
class Cow extends Animal{
public void stinky() {
System.out.println("stinky cow !");
}


now :

Animal a = new Dog();
a.stinky();

according first sentence should be printed : 'stinky animal !'
according second sentence should be printed -> 'stinky dog !' (this is printed in reality)

Is explanation in that book wrong or i made logical mistake somewhere ?

(sorry for my english)
Thanks
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
There is nothing wrong in the explanation.



What it meant was.

when you create object like this and call a method:

Animal obj = new Dog();
obj.stinky();

What compiler does is, it check the class type of obj which is Animal. After that it checks whether the stinky() exist in Animal or not. Always remember that objects are created at runtime. So complier has no way to determine that the Dog class stinky() method is to be called. So at compile time class type of reference variable is checked to check such a method exist or not.

Now at run time the jvm knows that though the class type of obj is Animal but at run time it is referring to the object of Dog. So it calls the stinky() of Dog class. This is called Dynamic Polymorphism.


"History would be kind to me, for I intend to write it."
Neo Neo
Greenhorn

Joined: Feb 25, 2009
Posts: 3
Suppose if I change the classes to as given below.


Animal a = new Dog();
a.stinky();

As per your point 1, you wont be able to call a.bark()

as per your point 2 if you call the stinky method, it will call the stinky in Dog.

class Animal {
void stinky(){
System.out.println("stinky animal !");
}
}

class Dog extends Animal{
public void stinky() {
System.out.println("stinky dog !");
}
public void bark(){
System.out.println("wow wow");
}

}
class Cow extends Animal{
public void stinky() {
System.out.println("stinky cow !");
}

Cheers,
Neo
lucas van derstappen wrote:I am reading now a SCJP5 study guide book (Osborne, 2006 - Sierra,Bates) and i am little confused.

Problem is in these two sentences : ( two minute drill chapter 2, pages 153,154)

Chapter about Polymorphism :
(I.) Reference variable`s type (not the object`s type) determines which methods can be called.

And

Chapter about overriding :
(II.) Object type (not reference variable`s type), determines which overriden method is used at runtime.


Just imagine :
class Animal {
void stinky(){
System.out.println("stinky animal !");
}
}

class Dog extends Animal{
public void stinky() {
System.out.println("stinky dog !");
}

}
class Cow extends Animal{
public void stinky() {
System.out.println("stinky cow !");
}


now :

Animal a = new Dog();
a.stinky();

according first sentence should be printed : 'stinky animal !'
according second sentence should be printed -> 'stinky dog !' (this is printed in reality)

Is explanation in that book wrong or i made logical mistake somewhere ?

(sorry for my english)
Thanks
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
No you wont be able to even compile in that case.
Beacuse at compile time the comiler sees that the class type of obj is Animal and there exist no method bark() in Animal class. Hence it will display a compile time error.
Neo Neo
Greenhorn

Joined: Feb 25, 2009
Posts: 3
Correct, but thaz what I too said :-)

Priyam Srivastava wrote:No you wont be able to even compile in that case.
Beacuse at compile time the comiler sees that the class type of obj is Animal and there exist no method bark() in Animal class. Hence it will display a compile time error.
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
yeah you did
hopefully you understood the explanation
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

lucas van derstappen wrote:
Chapter about Polymorphism :
(I.) Reference variable`s type (not the object`s type) determines which methods can be called.

And

Chapter about overriding :
(II.) Object type (not reference variable`s type), determines which overriden method is used at runtime.





Dear I think your question itself holds the answer.....


The reference variable, when of super type, the methods that can be called on that type is those that are defined in the super class. And when at run time which overridden method is called depends on the object to what the reference variable is actually pointing to. This is also known as dynamic bynding

Hope this helps


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Basically at compile time, the compiler is interested in the reference type, whilst at runtime the JVM is interested in the Object type (just in time compiling). It took me a while to get used to this, but after that its quite straight forward! Dont forget objects are only created at runtime, so before that you only have the compiler!


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: polymorphism, overriding - confusion ?