• Post Reply Bookmark Topic Watch Topic
  • New Topic

Covariant Return Type  RSS feed

 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All, I am trying to understand the Covariant return type.

I am unable to understand the definition of Covariant Return Type , as it says " the return types of the overriding methods will be subtypes of the return type of the overridden method".......I am unable to understand this English sentence.


However I did a sample .



I want to know,
1. What is the rule for creating Covariant Return Type overriding?
2. Why it is necessary in Java? What if it is not been introduced, were people getting any problem prior to this?
3. Is method Return type should be Class objects only?


Thanks,
Sharmistha
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharmistha Sarkar wrote:1. What is the rule for creating Covariant Return Type overriding?

Exactly what you saw and wrote above:
" the return types of the overriding methods [must] be subtypes of the return type of the overridden method"

2. Why it is necessary in Java? What if it is not been introduced, were people getting any problem prior to this?

Because it's very often the case that a subclass will want to enforce a more restrictive return type than its parent (usually its own). Prior to 1.5, clients had to explicitly cast the returned object if they wanted to do this.

3. Is method Return type should be Class objects only?

I'm afraid I don't understand the question.

Winston
 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston,

I am clear with my 2nd Question answer.

1. What is the rule for creating Covariant Return Type overriding?

Answer: the return types of the overriding methods [must] be subtypes of the return type of the overridden method



I know it's damn silly , but I really didn't understand the meaning of the sentence.

Let me elaborate it,

overriding methods meaning : Here from my above example, B print1() method = inside class B is Overriding method. It should return B class object.


overridden method meaning : A print1() method = inside Class A. It should return Class A Object or constructor but I am returning class B constructor.


Now if the above two are correct then just want to know; what is the meaning of " overriding methods [must] be subtypes ". What is that subtypes mean?


I am sorry for confusing you.

My 3rd Question is : whether Covariant Return Type would be class constructors or objects only? or it could be primitive data types like int,void etc...?
Example , I gave print1() with class A or class B return type...whether it could be int or float etc?


Regards,
Sharmistha
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are confusing yourself by using poorly‑named classes and returning the same type as the enclosing type. As Winston said, an overriding method is allowed to be more specialised than the overridden method. So its return type can be more specialised.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharmistha Sarkar wrote:My 3rd Question is : whether Covariant Return Type would be class constructors or objects only? or it could be primitive data types like int,void etc...?

Covariance refers to reference types, so no, you can't use it with primitive types.

To be honest, I forget if the language allows any latitude for different primitive return types between overridden methods, but I suspect not. And even if it did, I can't think of a reason why I'd ever need it.

I'm sure one of the sterling chaps here will correct me if I'm wrong though.

Winston
 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please tell me which one is the overriding method here and which one is overridden method?

Consider, this example.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharmistha Sarkar wrote:Can you please tell me which one is the overriding method here and which one is overridden method?

I suggest you have a think about it yourself. Alternatively, read the tutorials.

Question 1: Why do you think you might want to override a method?
Question 2: If you've answered Question 1, where is the overridden method likely to be?

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!