← Back to Core Java Course | Chapter 8: Inheritance | Lesson 7 of 7

Java के Covariant Return Types

एक covariant return type एक overriding method को parent method से ज़्यादा specific type return करने देता है, एक vehicle promise करने लेकिन एक bicycle deliver करने जैसा। यह अब भी एक fair match है।
Syntax
java
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?

java
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);
	}
}

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

java
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);
	}
}

Type Safety Benefits

Covariant returns के बिना, callers को वापस वह ज़्यादा specific type पाने के लिए एक explicit downcast चाहिए होगा जो वे जानते हैं असल में receive कर रहे हैं — covariant returns compiler को उस precision को अपने आप track करने देते हैं और type errors जल्दी पकड़ते हैं।

उदाहरण: Type Safety Benefits

java
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);
	}
}

Standard Library Covariants

Standard library में कई जगहें covariant returns उपयोग करती हैं, सबसे notably Object.clone(), जहां subclasses आमतौर पर clone() को अपने type return करने के लिए override करती हैं बजाय generic Object के जो parent method declare करता है।

उदाहरण: Standard Library Covariants

java
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);
	}
}

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

java
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());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक return type उपयोग करना जो original का subclass नहीं, जो एक compile error है।
  2. यह उम्मीद करना कि एक overriding method इसके parameter types बदलेगा, जब सिर्फ return type narrowed हो सकता है और एक अलग parameter list एक overload बनाता है।
  3. यह उम्मीद करना कि covariance parameter types के लिए काम करेगा, जब यह सिर्फ return types पर apply होता है।
चैप्टर सारांश
  • Inheritance एक class को दूसरी class के members reuse करने देता है, single, multilevel, और hierarchical forms में।
  • Method overriding एक subclass को inherited behavior replace करने देता है, और super parent class access करता है।
  • Covariant return types एक overriding method को ज़्यादा specific type return करने देते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.