• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Covariant Type doubt.

 
Suresh Rajadurai
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yet this will still work. Polymorphism only works for methods.



 
Campbell Ritchie
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bennett is correct. We have an FAQ about it; fields are not overridden, but hidden.
 
Suresh Rajadurai
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jesintha jegan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Suresh Rajadurai
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic