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

Java Inheritance परिचय

Inheritance एक नई class को एक पुरानी class से सब कुछ receive करने देता है, एक parent के traits inherit करने वाले child जैसा। Child अपनी खुद की चीज़ें जोड़ या बदल सकता है।
Syntax
java
class ParentClass {
    // parent members
}

class ChildClass extends ParentClass {
    // child members
}

What is Inheritance?

Inheritance एक class (subclass) को extends उपयोग करके किसी दूसरी (superclass) की fields और methods acquire करने देता है, इसलिए shared behavior सिर्फ parent में एक बार लिखना है और हर child class में अपने आप available है।

उदाहरण: What is Inheritance?

java
class Animal {
	void eat() {
		System.out.println("Eating");
	}
}
class Dog extends Animal {
}
public class Main {
	public static void main(String[] args) {
		new Dog().eat(); // inherited from Animal
	}
}

The super Keyword

super keyword एक subclass को इसके immediate parent की fields, methods, या constructors तक explicitly पहुंचने देता है — तब उपयोगी जब subclass को parent के behavior को पूरी तरह replace करने के बजाय extend करना हो।

उदाहरण: The super Keyword

java
class Animal {
	String type = "Animal";
}
class Dog extends Animal {
	String type = "Dog";
	void show() {
		System.out.println(super.type); // reaches the parent's field
	}
}
public class Main {
	public static void main(String[] args) {
		new Dog().show();
	}
}

Method Overriding

Overriding तब होता है जब एक subclass अपने parent से inherited एक method को फिर से define करता है, उस method को subclass के लिए specific एक नया implementation देते हुए जबकि parent ने declared किया वही method signature रखते हुए।

उदाहरण: Method Overriding

java
// Define the class Animal
class Animal {
	void sound() {
		// Print a line to the console
		System.out.println("Some sound");
	}
}
// Define the class Dog
class Dog extends Animal {
	// Override the method inherited from the parent type
	@Override
	void sound() {
		// Print a line to the console
		System.out.println("Bark");
	}
}
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		new Dog().sound();
	}
}

IS-A Relationship

Inheritance एक genuine "is-a" relationship model करने के लिए है — एक Dog असल में एक Animal है, एक Sedan असल में एक Vehicle है — और उन cases में inheritance तक पहुंचना जो इस pattern में fit नहीं होते आमतौर पर awkward, hard-to-maintain class hierarchies की ओर ले जाता है।

उदाहरण: IS-A Relationship

java
class Vehicle {}
class Sedan extends Vehicle {} // a Sedan IS-A Vehicle
public class Main {
	public static void main(String[] args) {
		Vehicle v = new Sedan();
		System.out.println(v instanceof Vehicle);
	}
}

Constructor Call Sequence

जब एक subclass object constructed होता है, parent class का constructor हमेशा पहले चलता है (implicitly, या explicitly super(...) के ज़रिए), और सिर्फ वह खत्म होने के बाद subclass की अपनी constructor body execute होती है।

उदाहरण: Constructor Call Sequence

java
class Animal {
	Animal() {
		System.out.println("Animal constructor");
	}
}
class Dog extends Animal {
	Dog() {
		System.out.println("Dog constructor");
	}
}
public class Main {
	public static void main(String[] args) {
		new Dog(); // Animal constructor runs first
	}
}
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. जब parent का कोई default constructor न हो तो super(...) call न करना, जो एक compile error है।
  2. Inheritance उपयोग करना जहां classes का "is-a" relationship नहीं।
  3. extends से एक से ज़्यादा class से inherit करने की कोशिश करना, जो Java allow नहीं करता।

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.