Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism question

 
Tim Storms
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check the following code (which is mine):

When I execute this:

I expected the result would be:

Do somingthing in B
static test in A
5
50

while the output was:

Do something in B
static test in A
100
50.

So I guess that all variables, also static ones, are always taken from the superclass. Am I correct? How can you explain this behavior?
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Tim Storms
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I stated before, I use polymorphism to instatiate the object. I'll include the main method this time:


Thanks for your advice.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,

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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you got it.
 
S Thiyanesh
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kishore kovil
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic