aspose file tools*
The moose likes Beginning Java and the fly likes Question regarding Polymorphism. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question regarding Polymorphism." Watch "Question regarding Polymorphism." New topic
Author

Question regarding Polymorphism.

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Please consider the code as follows:
The result is „Child.test“ as first line because anSub is of Base reference type
But of Child object type. When an instance method is overridden then the
object type counts if the method is called. Therefore Child.test() method
is called and printed. This behavour is called Polymorphism.
But then ssubObj is fully a a Base oject with „Base ssubObj = (Base)ansub.
So why is Child.test() still called, shouldn’t it be Base.test()?

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Casting doesn't affect the type of an object at all, only the type of a reference!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
NI Java Music
Greenhorn

Joined: Sep 12, 2002
Posts: 1
I think the problem you have identified is solved by understanding object referencing. (By the way I am probably more of a beginner than you so please feel free to have me contradicted!). You have created an object called anSub. But in the line:
Base ssubObj = (Base)anSub;
it seems to me that ssubObj is a reference to an object called asSub. So if you forget about casting for a moment, and assume you have one object called anSub with a reference ssubObj pointing to it, then its like having two of the exact same thing(but there is only one object). However you have casted the anSub to a class type (the superclass Base). It changes the type, but it doesn't change what is in the object that ssubObj points to namely your Child print statement.
As I say, I see it this way, but I am far from an expert. Please don't rely on what I am saying, but let me know if you agree or not.


NI Java Music
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

"NI Java Music"-
Welcome to the JavaRanch!
Please adjust your displayed name to meet the JavaRanch Naming Policy.
You can change it here.
Thanks, and again welcome to the JavaRanch!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by NI Java Music:
I think the problem you have identified is solved by understanding object referencing. (By the way I am probably more of a beginner than you so please feel free to have me contradicted!). You have created an object called anSub. But in the line:
Base ssubObj = (Base)anSub;
it seems to me that ssubObj is a reference to an object called asSub. So if you forget about casting for a moment, and assume you have one object called anSub with a reference ssubObj pointing to it, then its like having two of the exact same thing(but there is only one object). However you have casted the anSub to a class type (the superclass Base). It changes the type, but it doesn't change what is in the object that ssubObj points to namely your Child print statement.
As I say, I see it this way, but I am far from an expert. Please don't rely on what I am saying, but let me know if you agree or not.

You are nearly correct - in fact, even anSub is the name of a *reference*, the actual object it is pointing to doesn't have a name at all!
 
 
subject: Question regarding Polymorphism.
 
Similar Threads
Overriding
Another mock exam question.
call to shadowed static method at runtime!!!
super in constructors
Confused help me!!