This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Where is the main method in your class?? Any how are you calling the constructors?? By creating an object of Superclass or by creating an Object of the subclass??
The only thing that you have to know here is that, variables are called based on the reference type rather than object type. So in your example the reference is of type A which resulted in calling instance variables in A.
When it comes to overridden methods, decision is made during run time, so during run time, the actual object is of class B. There is no concept of static methods overriding, or private methods overriding. Hope you understood my brief explanation for your question.
Tim Storms
Ranch Hand
Joined: Apr 27, 2006
Posts: 64
posted
0
Thanks for your explanation!
Ok so, variables are always chosen based on the reference type (here it's A). The methods of the object type (B here) are used when an overriden method is called. And since static methods cannot be overriden, the static method of the reference type (A) is used. Is this correct?
Only the instance method overriding takes the run-time polymorphism and depends on the object type. All the other bindings are resolved at the compile time as per the reference type.
Sureshkumar Chinnappan
Greenhorn
Joined: Feb 15, 2006
Posts: 27
posted
0
Could any one please explain how the memory allocated for insatnce variables in the below ex?(in depth)
Class Animal{ int a=10; }
Class Dog extends Animal{ int a=20; int b=20; public static void main(String args[]){ Animal animal = new Dog(); System.out.println(animal.a); System.out.println(animal.b);
} } In the above example how the memory location pointing to the refernce type in animal.a but object type for animal.b
Hi Suresh, The prog results in compile time error. you can refer from the above mails that variables are allocated memory based on reference you are refering with.As 'b' doesn't belong to SuperClass animal, it results in compile time error. Thanks, Kishore
Sureshkumar Chinnappan
Greenhorn
Joined: Feb 15, 2006
Posts: 27
posted
0
Hi kishore/All,
Please find the correct program below:
class Animal{ int a=10; public String printValue(){ return "Printing this from Animal"; } }
public class Dog extends Animal{ int a=20;
public String printValue(){ return "Printing this from Dog"; }
public static void main(String args[]){ Animal animal = new Dog(); System.out.println(animal.a); System.out.println(animal.printValue());
} } And the output is 10 Printing this from Dog
How the memory allocated in this program?
Sanjeev Singh
Ranch Hand
Joined: Nov 01, 2006
Posts: 381
posted
0
You can make it out using the rules. * Instance variables and objects live on the heap. * Local variables live on the stack.
Find out which codes runs and created objects,instance variable etc. [ January 12, 2007: Message edited by: Sanjeev Kumar Singh ]
~Sanjeev Singh<br />SCJP 1.5
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.