aspose file tools*
The moose likes Java in General and the fly likes 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 » Java in General
Bookmark "Polymorphism" Watch "Polymorphism" New topic
Author

Polymorphism

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
So can someone explain why this is the way it is and what general rule applies to this
Class X is the base class
Class Y extends X
the doStuff() method exists in X
The doStuff() method is overriden in Y
Why do I get class Y's doStuff method executing when I run the following code?
public static void main(String[] args)
{
X myX = new Y();
myX.doStuff();
}
-Dale

------------------
What's this H2SO4 doing in my fridge?? ( thud )


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
First of all, since Y is a subclass of X, you an assign an instance of Y to a reference type X.
Consider the statement X myX = new Y();
Eventhough myX is has a declared type of 'X', it has a runtime type of 'Y'. At runtime, any method invocation on myX will actually get resolved from the runtime type, not the declared type. Hence myX.doStuff() calls the method on the object of type Y.
When the method is invoked, a bottom-up approach is used to resolve the method if inheritance is involved. Starting from the current class, the Virtual Machine code looks for methods that match the specific signature. If none found, the same search is executed recursively on all the classes in the hierarchy until one found, or you reach the primordial java.lang.Object clas.
In your case, since Y overrides the doStuff() method in X, the overridden version is executed.
Hope that helps!
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
[This message has been edited by Ajith Kallambella (edited July 10, 2001).]


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
So in a sense I can use this for polymorphism. I can declare an array of type X and put in objects Y Z and A as long as then extend from X as the base. Then I could use all of the common methods in Y Z and A to do my work as I traversed through the array
for (int i; i<10; i++)
{
myObject[i].calcFinalTotal();
}
Rather than declaring the array to have an interface type. Is this correct?
-Dale
------------------
What's this H2SO4 doing in my fridge?? ( thud )
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
But don't forget, since the type of the reference variable myX is X anything that is not resolved at runtime will have the value that X would have because of early binding.
Therefore all of the variables of myX are X variables even though the methods of myX will pick up the methods of Y.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Cindy,
Referring to your reply
Cindy Said:
But don't forget, since the type of the reference variable myX is X anything that is not resolved at runtime will have the value that X would have because of early binding.
Therefore all of the variables of myX are X variables even though the methods of myX will pick up the methods of Y.
Are you saying this is true for ALL classes that are extended from a base class? Or are there instances where this would not happen.
-Dale
------------------
What's this H2SO4 doing in my fridge?? ( thud )
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
In all cases the variables used by the reference variable will be of the type of the reference variable (in this case X) and the methods used will be of the type of the object referenced by the variable (in this case Y);
Many times the variable holds a reference to an object of the same type that it is, so you don't SEE the difference. Only when the variable is of a super type or an interface type do you notice this fact.
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515

So the next question is this then... given the above information,
"
But don't forget, since the type of the reference variable myX is X anything that is not resolved at runtime will have the value that X would have because of early binding.
"
does it still hold true that the private variables will not be accessable in the superclass???
-Dale
------------------
What's this H2SO4 doing in my fridge?? ( thud )
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The variable myX will contain a field for the private variable of the X class. The object which is of type Y will not be able to access the private field.
Of course you could have tried this yourself . . . but given the following 2 classes:

and

You will get the following results on compiling.
No variable childPrivateString defined in Base.
No variable childPublicString defined in Base.
Variable privateString in class Base not accessible from Child class.
parind poi
Greenhorn

Joined: Jun 19, 2001
Posts: 9
Dale,
I was mistaken in what I posted yesterday( Hence the deletion of my post).
The reality is:
There is no way to get at base class's method if derived class has overridden it. Either base or derived would have had to given you a backdoor to that method with a wrapper around it by another name that was not overridden. Derived can of course use super to get at base class's methods.
Look at "OverRide Vs Shadow" at http://mindprod.com/gotchas.html
Parind
 
 
subject: Polymorphism
 
Similar Threads
Wrappers
accessing shadowed final values in method local inner class
Daught
Method overloading
Gotcha: Varargs spoofs method signature [from Sierra/Bates book]