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()?
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
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"- 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
posted
0
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!