Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Co variant returns

 
harmeet saini
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi follwing is the problem from certPal

Given this code snippet, what can be inserted in the fragment ?


And the otipns are
1)return new Object();
2)return this;
3)return new Marmaduke();
4)return null;
5)return"";

the correct options are 2,3,4 and the expalnation given is "Co variant returns are allowed in java language as of JDK1.5

I am not able to understand the explanation and also why option 1 is not correct

Thanks in advance
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot return an Object when the expected return type is a subclass of Object.

As for the co-variant return type, of what class type is the instance 'this' ? What is the difference between 'this' and Marmaduke ?
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think this has anything to do with co-variant return types. Its just polymorphism. The return type of go method is Marmaduke, so you cannot return an Object from it. I would be like doing this


(PS: The spelling of otipns is great )
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think this has anything to do with co-variant return types. Its just polymorphism


Whoops. That is indeed correct. Will make a minor change to the answer's explanation
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh and thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic