Java Inheritance
What is Inheritance?
Inheritance is a mechanism in Java where one class acquires the properties (fields) and behaviors (methods) of another class. The class which inherits is called the subclass or child class, and the class from which it inherits is called the superclass or parent class.
Inheritance Java की एक ऐसी सुविधा है जिसमें एक class दूसरी class की properties (fields) और behaviors (methods) को प्राप्त करती है। जो class properties लेती है उसे subclass या child class कहते हैं और जिससे properties ली जाती हैं उसे superclass या parent class कहते हैं।
Advantages of Inheritance
- Code Reusability: Reuse existing code without rewriting.
- Method Overriding: Customize behavior in subclasses.
- Improved Code Maintenance: Changes in superclass reflect in subclasses.
- Hierarchical Classification: Represents real-world relationships.
- Code Reusability: Existing code को दोबारा लिखे बिना reuse करना।
- Method Overriding: Subclass में behavior को customize करना।
- Improved Code Maintenance: Superclass में changes सब subclasses में apply होते हैं।
- Hierarchical Classification: असली दुनिया के संबंधों को दर्शाता है।
Types of Inheritance in Java
- Single Inheritance: One subclass inherits one superclass.
- Multilevel Inheritance: A subclass inherits from a superclass, which itself inherits from another superclass.
- Hierarchical Inheritance: Multiple subclasses inherit from one superclass.
- Hybrid Inheritance: Combination of two or more types of inheritance (achieved using interfaces in Java).
- Single Inheritance: एक subclass एक superclass से inheritance लेता है।
- Multilevel Inheritance: एक subclass दूसरी subclass से और वो superclass से inheritance लेता है।
- Hierarchical Inheritance: कई subclasses एक superclass से inheritance लेते हैं।
- Hybrid Inheritance: inheritance के दो या अधिक प्रकारों का संयोजन (Java में interfaces से पूरा किया जाता है)।
Why Java Does Not Support Multiple Inheritance
Java does not support multiple inheritance (a class inheriting from multiple classes) to avoid ambiguity and complexity, especially the "Diamond Problem" where methods from multiple superclasses clash. Instead, Java supports multiple inheritance via interfaces to provide flexibility without such issues.
Java multiple inheritance (एक class का कई classes से inheritance) को ambiguity और complexity से बचने के लिए support नहीं करता, खासकर "Diamond Problem" के कारण जहाँ एक से अधिक superclass के methods टकरा जाते हैं। इसलिए Java में multiple inheritance interfaces के माध्यम से किया जाता है।
Examples of Inheritance with Employee Class
Single Inheritance Example
class Employee {
String name;
int id;
Employee(String name, int id) {
this.name = name;
this.id = id;
}
void display() {
System.out.println("Name: " + name);
System.out.println("ID: " + id);
}
}
class Manager extends Employee {
String department;
Manager(String name, int id, String department) {
super(name, id);
this.department = department;
}
void displayManager() {
display();
System.out.println("Department: " + department);
}
}
public class Test {
public static void main(String[] args) {
Manager m = new Manager("Ramesh", 101, "Sales");
m.displayManager();
}
}
Output:
Name: Ramesh ID: 101 Department: Sales
Manager inherits Employee and adds department field.
Manager class Employee से inheritance लेती है और department field जोड़ती है।
Multilevel Inheritance Example
class Employee {
String name;
int id;
Employee(String name, int id) {
this.name = name;
this.id = id;
}
void display() {
System.out.println("Name: " + name);
System.out.println("ID: " + id);
}
}
class Manager extends Employee {
String department;
Manager(String name, int id, String department) {
super(name, id);
this.department = department;
}
void displayManager() {
display();
System.out.println("Department: " + department);
}
}
class SeniorManager extends Manager {
int teamSize;
SeniorManager(String name, int id, String department, int teamSize) {
super(name, id, department);
this.teamSize = teamSize;
}
void displaySeniorManager() {
displayManager();
System.out.println("Team Size: " + teamSize);
}
}
public class Test {
public static void main(String[] args) {
SeniorManager sm = new SeniorManager("Suresh", 102, "Marketing", 10);
sm.displaySeniorManager();
}
}
Output:
Name: Suresh ID: 102 Department: Marketing Team Size: 10
SeniorManager inherits Manager, which inherits Employee.
SeniorManager class Manager से और Manager class Employee से inheritance लेती है।
Hierarchical Inheritance Example
class Employee {
String name;
int id;
Employee(String name, int id) {
this.name = name;
this.id = id;
}
void display() {
System.out.println("Name: " + name);
System.out.println("ID: " + id);
}
}
class Manager extends Employee {
String department;
Manager(String name, int id, String department) {
super(name, id);
this.department = department;
}
void displayManager() {
display();
System.out.println("Department: " + department);
}
}
class Developer extends Employee {
String programmingLanguage;
Developer(String name, int id, String programmingLanguage) {
super(name, id);
this.programmingLanguage = programmingLanguage;
}
void displayDeveloper() {
display();
System.out.println("Programming Language: " + programmingLanguage);
}
}
public class Test {
public static void main(String[] args) {
Manager m = new Manager("Ramesh", 101, "Sales");
Developer d = new Developer("Neha", 103, "Java");
m.displayManager();
System.out.println();
d.displayDeveloper();
}
}
Output:
Name: Ramesh ID: 101 Department: Sales Name: Neha ID: 103 Programming Language: Java
Manager and Developer both inherit Employee.
Manager और Developer दोनों Employee class से inheritance लेते हैं।
Hybrid Inheritance Example (using Interfaces)
interface Payable {
void calculatePay();
}
interface Bonusable {
void calculateBonus();
}
class Employee implements Payable, Bonusable {
String name;
Employee(String name) {
this.name = name;
}
public void calculatePay() {
System.out.println(name + " - Calculating Pay");
}
public void calculateBonus() {
System.out.println(name + " - Calculating Bonus");
}
}
public class Test {
public static void main(String[] args) {
Employee e = new Employee("Amit");
e.calculatePay();
e.calculateBonus();
}
}
Output:
Amit - Calculating Pay Amit - Calculating Bonus
Employee class implements two interfaces to achieve hybrid inheritance.
Employee class दो interfaces को implement करती है, जो hybrid inheritance दिखाता है।