my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Polymorphism question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Polymorphism question" Watch "Polymorphism question" New topic
Author

Polymorphism question

Tim Storms
Ranch Hand

Joined: Apr 27, 2006
Posts: 64
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?


SCJP 1.5, SCWCD 1.4, SCBCD 5
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

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??


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Tim Storms
Ranch Hand

Joined: Apr 27, 2006
Posts: 64
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

Joined: Sep 26, 2006
Posts: 9345
    
    2

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

Joined: Apr 27, 2006
Posts: 64
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

Joined: Sep 26, 2006
Posts: 9345
    
    2

Yes you got it.
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
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
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


Warm Regards,<br />Suresh Kumar<br /> <br />SCJP 5.0<br />SCWCD 1.4
kishore kovil
Greenhorn

Joined: Dec 21, 2006
Posts: 20
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
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
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Polymorphism question
 
Similar Threads
Assignment doubt
Short initialize
static objects
static variables are not accesed by class instances????
Problem related to enums