• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regarding Polymorphism.

 
Thomas Markl
Ranch Hand
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casting doesn't affect the type of an object at all, only the type of a reference!
 
NI Java Music
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic