jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem in polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem in polymorphism" Watch "Problem in polymorphism" New topic
Author

Problem in polymorphism

Poornima Sharma
Ranch Hand

Joined: Sep 09, 2008
Posts: 114
Hi friends,
The output of this program is 5
but according to me it should be 6.
Please help.
Thanks in Advance.

public class CovariantTest
{
public A getObject()
{
return new A();
}
public static void main(String []args)
{
CovariantTest c1=new SubCovariant();
System.out.println(c1.getObject().x);
}

}
class A
{
int x=5;
}
class B extends A
{
int x=6;
}
class SubCovariant extends CovariantTest
{
public B getObject()
{
return new B();
}
}


Poornima Sharma
SCJP 6.0
Sudarshan Chakrabarty
Ranch Hand

Joined: Apr 10, 2008
Posts: 38
The result is correct.
It would be 5,
because instance variables ( as well as static methods) cannot be overridden.

Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
Posts: 103
Hi Poornima

Is it sure that the last method definition is

public B getObject()
{
return new B();
}

because, you tried to return a subclass of A in the overriden method. You can't narrow the return. here you had done the narrowing(From A to B).

Then about your question, we are taking the value of x, using the refence of A. so it will take the data declared in class x.

To get a better idea on this, change the variable name in B to y, and try to access that from maain method, as you did.


Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
Poornima Sharma
Ranch Hand

Joined: Sep 09, 2008
Posts: 114
Yes, The question is correct.
What i am unable to understand is
c1.getObject() runs the subcovariant's method
but returns Class A where it should return Class B
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Yes the getObject() method called returns an object of B, but the getObject method in SubCovariant has changed the return type of the getObject method in CovarientTest class from A to B.

So for the JVM the statement

System.out.println(c1.getObject().x);

looks like this

System.out.println(((A)c1.getObject()).x);

You can understand it better if you split the confusing line like this



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
Poornima, the code provided by you gives compiler error as the return type of method getObject() in class SubCovariant is B. If you make it A and keep the remaining code intact as
class SubCovariant extends CovariantTest
{
public A getObject()
{
System.out.println("B");
return new B();
}
}

when you run the code, you will find that the method is overridden so it will call the SubCovariant's method but when accessing instance variables, the reference type determines whose variable will be accessed i.e (CovariantTest c1) will access class A's x. Thats y x=5.

Hope this helps.
[ September 22, 2008: Message edited by: vidhya suvarna ]

SCJP 1.4 - 88%<br />SCWCD 1.5 - Preparing
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Originally posted by vidhya suvarna:
Poornima, the code provided by you gives compier error


I think you are using JDK 1.4 or older. The code runs fine on my PC....
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
yes, its JDK 1.4
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

@vidhya if you are preparing for SCJP 1.5 or 1.6, then you must use JDK 1.5 or 1.6 respectively.....Otherwise you can get into deep troubles...
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
Ankit, am planning for SCJP 1.4 this week.
Mohammad Khan
Ranch Hand

Joined: Sep 23, 2008
Posts: 37
Let make it little confusing for you,

Add the following method in class A and B

public void display() {
System.out.println("Class X"); // change X to A or B
}

and add the following line at the end of main method

c1.getObject().display();

Thanks,
V.G. Hindriksen
Greenhorn

Joined: Sep 15, 2008
Posts: 9
A good way to understand this is to change the code a little:


This still compiles and the answer will be 6. Just change this part a few times and you'll get the idea of shadowing.
Farrukh Mahmud
Greenhorn

Joined: Sep 25, 2008
Posts: 1
found the following in the kb book

Polymorphic method invocations apply only to instance methods. You can
always refer to an object with a more general reference variable type (a superclass
or interface), but at runtime, the ONLY things that are dynamically selected
based on the actual object (rather than the reference type) are instance methods.
Not static methods. Not variables. Only overridden instance methods are
dynamically invoked based on the real object's type.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in polymorphism
 
Similar Threads
Explain the ouput
overriding with variables - whizlabs
Overriding question
Polymorphism
Covariant Return Types