Java के Covariant Return Types
In this page:
class Parent {
ParentType methodName() { }
}
class Child extends Parent {
@Override
ChildType methodName() { } // ChildType extends ParentType
}
What is a Covariant Return Type?
एक covariant return type एक subclass में एक overriding method को इसके override किए parent method से ज़्यादा specific return type declare करने देता है, जब तक वह ज़्यादा specific type खुद parent के declared return type का एक subtype हो।
उदाहरण: What is a Covariant Return Type?
class Animal {}
class Dog extends Animal {}
class AnimalShelter {
Animal adopt() { return new Animal(); }
}
class DogShelter extends AnimalShelter {
@Override
Dog adopt() { return new Dog(); } // more specific return type
}
public class Main {
public static void main(String[] args) {
System.out.println(new DogShelter().adopt() instanceof Dog);
}
}
Login to try C/C++/Java/PHP code in the editor
Covariant Returns with Custom Classes
यह factory-style methods के लिए विशेष रूप से उपयोगी है: एक parent class का clone()-जैसा method general parent type return कर सकता है, जबकि एक subclass override बिना किसी manual casting के callers को ज़्यादा precise type देते हुए सीधे अपना specific subclass type return कर सकता है।
उदाहरण: Covariant Returns with Custom Classes
class Shape {
Shape create() { return new Shape(); }
}
class Circle extends Shape {
@Override
Circle create() { return new Circle(); } // factory-style covariant return
}
public class Main {
public static void main(String[] args) {
Circle c = new Circle().create();
System.out.println(c instanceof Circle);
}
}
Login to try C/C++/Java/PHP code in the editor
Type Safety Benefits
Covariant returns के बिना, callers को वापस वह ज़्यादा specific type पाने के लिए एक explicit downcast चाहिए होगा जो वे जानते हैं असल में receive कर रहे हैं — covariant returns compiler को उस precision को अपने आप track करने देते हैं और type errors जल्दी पकड़ते हैं।
उदाहरण: Type Safety Benefits
class Animal {}
class Dog extends Animal {}
class DogShelter {
Dog adopt() { return new Dog(); }
}
public class Main {
public static void main(String[] args) {
Dog d = new DogShelter().adopt(); // no downcast needed
System.out.println(d);
}
}
Login to try C/C++/Java/PHP code in the editor
Standard Library Covariants
Standard library में कई जगहें covariant returns उपयोग करती हैं, सबसे notably Object.clone(), जहां subclasses आमतौर पर clone() को अपने type return करने के लिए override करती हैं बजाय generic Object के जो parent method declare करता है।
उदाहरण: Standard Library Covariants
class Point implements Cloneable {
int x = 5;
@Override
public Point clone() {
try {
return (Point) super.clone(); // covariant: returns Point, not Object
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
}
public class Main {
public static void main(String[] args) {
Point p = new Point();
Point copy = p.clone();
System.out.println(copy.x);
}
}
Login to try C/C++/Java/PHP code in the editor
Rules for Covariant Returns
यह rule सिर्फ reference types पर apply होता है, primitives पर नहीं — एक double-returning method को एक int return करने के लिए override नहीं किया जा सकता, भले ही int values double में convert हो सकें, क्योंकि covariance strictly reference types के बीच subtype relationships के बारे में है।
उदाहरण: Rules for Covariant Returns
class Base {
Object getValue() { return "base"; }
}
class Derived extends Base {
@Override
String getValue() { return "derived"; } // String is a subtype of Object
}
public class Main {
public static void main(String[] args) {
System.out.println(new Derived().getValue());
}
}
Login to try C/C++/Java/PHP code in the editor
- एक return type उपयोग करना जो original का subclass नहीं, जो एक compile error है।
- यह उम्मीद करना कि एक overriding method इसके parameter types बदलेगा, जब सिर्फ return type narrowed हो सकता है और एक अलग parameter list एक overload बनाता है।
- यह उम्मीद करना कि covariance parameter types के लिए काम करेगा, जब यह सिर्फ return types पर apply होता है।
- Inheritance एक class को दूसरी class के members reuse करने देता है, single, multilevel, और hierarchical forms में।
- Method overriding एक subclass को inherited behavior replace करने देता है, और
superparent class access करता है। - Covariant return types एक overriding method को ज़्यादा specific type return करने देते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: