wood burning stoves*
The moose likes Beginning Java and the fly likes Covariant Type doubt. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Covariant Type doubt." Watch "Covariant Type doubt." New topic
Author

Covariant Type doubt.

Suresh Rajadurai
Ranch Hand

Joined: Feb 22, 2007
Posts: 58
Hi folks,

please have a look at the following code:




When I compiled and ran this code , the output is:

its B from subcovtest
5.

Even though its returning the B object , why its NOT returning x value from B, i.e 6?

Is the object being returned from "t.getObject()" (in line //*)
similar to the object created by the command "B b = new B()" ? or "A b = new B()" ?



much appreciated and thanks in advance.


Suresh
Manish R Singh
Greenhorn

Joined: Mar 14, 2009
Posts: 14
Sureshwa listen wht your doing is known as technique called Upcasting

covtest t = new subcovtest();

passing a subclass object to parent reference but parent reference has knowledge only of its own members does not have any knowledege of child members parent reference cannot access members specific to child object got it

now why are you getting this output "its B from subcovtest" that is because the CovTest class getObject() method is getting hidden by SubCovtest class method getObject(). so

And please write class name s in Capital !
don't write in small letters its really annoying!

samaz gaya kya babua!
Piyush Porwal
Ranch Hand

Joined: Apr 09, 2008
Posts: 30
covtest t = new subcovtest();
System.out.println(t.getObject().getX());

Yes, this type of reference to a method or instance variable is resolved at compile time itself. If you want to get the correct result using polymorphisms then just add getter method in A and B.



-Piyush Porwal (Junk box)
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
Yet this will still work. Polymorphism only works for methods.



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Ryan Bennett is correct. We have an FAQ about it; fields are not overridden, but hidden.
Suresh Rajadurai
Ranch Hand

Joined: Feb 22, 2007
Posts: 58
Sorry,

I dont understand the explanation.

My question is,

if t.getObject() is returning "Object B", then t.getObject().x is supposed to return 6 and NOT 5.


thanks


Suresh.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Suresh Rajadurai wrote:

if t.getObject() is returning "Object B", then t.getObject().x is supposed to return 6 and NOT 5.


No. An object of two B contains two different variables, both named "x". If you have a variable "a" of type "A", and it points to a "B" object, then "a.x" will give you the "x" from class A, not the one from class B. If on the other hand you have a variable named "b" of type "B', and it points to THE SAME B object, then "b.x" will give you the OTHER variable x, the one declared in class "B".

You get this strange behavior because, as has already been pointed out, variables are NOT polymorphic. A variable with the same name in a subclass hides the parent variable, rather than overriding it.

The correct way to write your class B, by the way, is



Note that we say "x=6", not "int x = 6". We're setting the value of the variable that already exists.


[Jess in Action][AskingGoodQuestions]
jesintha jegan
Greenhorn

Joined: Jan 05, 2009
Posts: 17
Please do correct me If I am wrong..

The instance variables are not overriden as they belong to class hence their value is determined on the basis of class instantiated at the compile time.

polymorphism works only for methods.

please have a look at the following code.

if you compile this means you will get the output as "parent".

In this case the yo object will reflect "Parent"(eventhough its a child object) because the instance variables are not overriden.

Hope i am clear.


If you don't design your own life plan, chances are you will fall into someone else's plan.
Suresh Rajadurai
Ranch Hand

Joined: Feb 22, 2007
Posts: 58
Hi Ernest,

So according to you,

the object returned by the statement t.getObject() is similar to object created by the statement
A obj = new B() ? Am I right?


for my explanation, please have a look at the following code:



Since my original program (my originial posting in this thread (far above) ) gives the output as 5 , I assume the object returned by the statement t.getObject() is similar to the object created in line //declaration Q above? Am I right?

I am realy confused.


Suresh.
Adi Kulkarni
Ranch Hand

Joined: Mar 12, 2009
Posts: 86
Hello ,

These scenarios are pretty confusing always.

Say you've got a class A . Class B extends A.
Class A has a method doStuff(){} and an instance variable 'x' and class B overrides the method doStuff(){} and instance variable 'x'

Whenever you declare something like. A a = new B() ;
and call "a.dostuff().x" which is a polymorphic call , the compiler will check if the class A has a method by name doStuff() , if it does have it will compile . At runtime the VM will call the overridden method since the object created at runtime is that of B. Hence the method called will be the over ridden one , but now as somebody has already pointed out that instance methods are not overridden , the call
a.doStuff().x will print the variable from Class A since the reference belongs to Class A i.e the parent class.

So for instance variables the reference type is very important.
Also naming the variables is a very important aspect in the code as there is one more phenomenon known as "shadowing" of variables which can lead to confusion.

~Aditya



SCJP 1.5
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Suresh Rajadurai wrote:

Since my original program (my originial posting in this thread (far above) ) gives the output as 5 , I assume the object returned by the statement t.getObject() is similar to the object created in line //declaration Q above? Am I right?

Suresh.


Your example will indeed work as you predict, and it prints what it does for the same reason that your original program prints what it does.
 
Consider Paul's rocket mass heater.
 
subject: Covariant Type doubt.