File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism

 
Dale DeMott
Ranch Hand
Posts: 515
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Dale DeMott
Ranch Hand
Posts: 515
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dale DeMott
Ranch Hand
Posts: 515
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 515
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic