• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in polymorphism

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The result is correct.
It would be 5,
because instance variables ( as well as static methods) cannot be overridden.

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Poornima Sharma
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, its JDK 1.4
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit, am planning for SCJP 1.4 this week.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic