• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method used at Runtime

 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below is a snippet of code. Assume that everything else in the code works fine, and this code runs fine also. Will this execute the method of this instance's getWealth, STParent's getWealth method, or STGrandParent's getWealth method? and Why did it execute that method?
((STParent)this).getWealth();
((STGrandParent)this).getWealth();
Assume that STGrandParent is the superclass of STParent which is the superclass of this.
For a given object, is the method associated with that object the same as the physical object that was created. I mean that there could be all types of variables pointing at an object, but the actual object or physical object would be what was originally created. The physical object will have methods associated with it. The variables could have methods of the same name, but would the method on the object that would be used be the method associated with the physical object originally created?
I would very much appreciate someone clarifying this point.
 
Lee Clarke
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charlie,
I whipped up an example of this myself and it would certianly call the method belonging to this nomatter what you cast it to. The reason it does this is because you have overridden the method localy. If you had not overriden it then it would have called the parents or GParents method. It goes back to the rule that if B is a subclass of A then
A myA = new B();
myA.someMethod() calls someMethod() in B.
Now if you access a member variable it will return the Parents value.
Hope thats of some help!
Lee
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you clarify the reason as to why? The part I am confused on is that the object is being cast into GrandParent or Parents, and after it is cast what determines which method it will use?
 
Lee Clarke
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Humm OK I'll give it a try but Im not sure how well I'll do.
I think it goes like this (and someone please correct me if Im wrong!)
The reason has something to do with when methods are determined for the object which I believe is when the object it instanciated. (could be inaccurate here)
Esentually, even though you have casted the object to be a parent it is still a subclass in its methods because that's what it was created as. If you want to call the parents method you would call super.someMethod() as for the Grand Parents method, it is not accessable from 2 levels down, inotherwords you cant call super.super.someMethos().
I have a feeling Im not giving you the best answer, I hope someone else can expand on this..
Lee

 
Gaurav Mantro
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for this behaviour is related with how Java implements the inheritance. When an object of subclass is created it also creates object of the super class and hence a Method table of the super class is also created. So once we start creating an object, the method table will contain entries for the super class but if you override a method in the sub class then the method table will override the superclass's method with subclass's method. So the object no longer contains the entry for super class's method.
As it is little bit confusing let me try to use an example
class SuperGrand
{
void PrintMe();
}
class Super extends SuperGrand
{
void PrintMe();
}
class Sub extends Super
{
void PrintMe()
}
So when object of Sub is created it will first create an object of Super. Super will try to create an object of SuperGrand before it gets created. So the order of creation of objects would be like
step1. SuperGrand
step2. Super
step3. Sub
So the method table at different steps will be
step1.
SuperGrand.PrintMe()
step2.
Super.PrintMe()
step3
Sub.PrintMe()
So as you can see everytime you override a method it changes the Method table. It's the Method table owned by the object so irrespective of the way you cast it you are looking at the same method table in the object and you have overridden the methods available in that Method table.
Another thing to note here is that when you cast an object it limits the method table entries that you can access depending on the type of object. That's why following code won't work
class Super{}
class Sub extends Super()
{
public static void Main(String [] args)
{
Sub objSub = new Sub();
((Super)objSub).InvisibleToSuper(); -> This won't compile.
}
public InvisibleToSuper()
{
}
}
I hope I was able to explain it. I would appreciate a correction if I am wrong. I am not sure if Method Table is the right word for it, but my intention was to explain my understanding and Method Table seemed to be a good name to explain.

hope it helps
Gaurav Mantro
[This message has been edited by Gaurav Mantro (edited February 02, 2001).]
 
natarajan meghanathan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have the foll. code to help u. Its a recursive call, do bear with me for that but it will explain the concept u wanted. Eventhough u casted the subclass reference to the superclass reference, it depends on the object to which the reference it is pointing to, that will the determine which method is to be executed.
class A{

void method(){

System.out.println(" Method A called..");
}

}
class B extends A{

void method(){

System.out.println(" Method B called..");

}
}

class C extends B{

void method(){

System.out.println(" Method C called..");

((B)this).method();
((C)this).method();



}

}
class inheritance{

public static void main(String[] args){

new C().method();
}
}
Hope this helps.
natarajan
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanations!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic