• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Covariant return, Whizlab question

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This is the question from the WhizLab



As per the rule of covariant return the output I find is 6. But the result given in the source of question says the output 5 and the reasoning of that answer seems wrong as well.

My reasoning says: "At runtime it is decided which getX() to call on behalf of the object type that is SubCovariant".

Obviously getX() of the SubCovariant class will be called that returns reference to the class B object. B class has x with the value 6, so therefore 6 is printed there.

Please suggest the way to think about this if I miss something.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer should be 6.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What was their reasoning?

You are correct that the correct version of the method to call is decided based on the type of the object since getX() is an instance method that is overridden.

The getX() method from B is called, but it is called through a method added to B by the compiler, so the object returned is of runtime type B, but reference type A.

Since toString() is an overridden method, the version of toString() in the runtime class of the object is called.

So the answer should be 6.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith,

I get you that the subclass method is called through a method added by the compiler to handle covariant return issue. We know it by bridge method.


What was their reasoning?



What Whizlab says:


Overriding is only for methods, not for variables. Variables are always chosen at compile time, depending on the declared type of the invoking object. Here the return type of obj.getX() is class A, so the variable access will be class A, and not class B. There are no compiler errors or exceptions here, since it is perfectly legal example of Covariant return types, i.e., the return type of the overriding method is a subclass of the return type of the overridden method.



What do you say about this Keith ?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,

Actually what the Whizlab question was talking about




My reasoning is correct if you write



Sorry for inconvenience that question might have caused you people.
BTW, question was interesting.
 
Ranch Hand
Posts: 37
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does this code compile, cause the return type of the overriden methods are not same? I gives me compile time error.
here return type B is not comptible with the return type of SuperCovariant getX() method's A. (I know that B is subclass of A, but compiler complains)
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Covariant return is allowed since Java 5.0;


This says, an overriding method can change the return type
but that type must be subclass of the return type of the base
class method.

See the example:




Thanks,
[ May 07, 2007: Message edited by: Chandra Bhatt ]
 
Sanjay Singh
Ranch Hand
Posts: 37
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh its again Java5.0. then it makes me clear. Thanks
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {
int x=5;
public String toString()
{return String.valueOf(x);
}}
class B extends A {
int x=6;
public String toString() {
return String.valueOf(x);
}}
public class SuperCovariant {
A getX() {
return new A();
}
public static void main(String args[]) {
SuperCovariant obj = new SubCovariant();
System.out.println(obj.getX());
}}
class SubCovariant extends SuperCovariant{
A getX() {
return new B();
}
}
see the above code my friend. you will get clear idea...
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic