Inheritance is a fundamental concept in object-oriented programming that allows a new class to inherit properties and behaviours from an existing class. There are several types of inheritance:
Single Inheritance
A class inherits from only one superclass.Single Inheritance Promotes simplicity and avoids ambiguity in method resolution.
class Person {
// Person class members and methods
}
class Programmer extends Person {
// Programmer class members and methods
}
Code language: JavaScript (javascript)
Multiple Inheritance
- A class inherits from more than one superclass.
- Can lead to the “diamond problem,” where ambiguity arises in method resolution.
- Due to this reason in java multiple inheritance achieved only using interfaces.
interface Programmer {
void becomeProgrammer();
}
interface Tester {
void becomeTester();
}
class Engineer implements Coder, Tester {
//Engineer class implements Programmer and Tester classes
}
Code language: PHP (php)
Multilevel Inheritance
- A class is derived from a class, which is already derived from another class.
- Creates a chain of inheritance.
class Person {
// Person class members and methods
}
class Programmer extends Person {
// Programmer class extending Person
}
class Engineer extends Programmer {
// Programmer class extending Engineer
}
Code language: JavaScript (javascript)
Hierarchical Inheritance
- Multiple classes inherit from a single superclass.
- Forms a hierarchy where multiple classes share common properties from a single base class.
class Person {
// Person class members and methods
}
class Programmer extends Person {
// Programmer class extending Person
}
class Tester extends Person {
// Tester class extending Person
}
Code language: JavaScript (javascript)
Here’s an example that demonstrates various inheritance forms.
// Base class class Person { String name; int age; Person(String name, int age) { this.name = name; this.age = age; } void displayDetails() { System.out.println("Name: " + name); System.out.println("Age: " + age); } } // Single Inheritance class Programmer extends Person { String programmingLanguage; Programmer(String name, int age, String programmingLanguage) { super(name, age); this.programmingLanguage = programmingLanguage; } void displayProgrammingDetails() { System.out.println("Programming Language: " + programmingLanguage); } } // Hierarchical Inheritance class Engineer extends Person { String specialization; Engineer(String name, int age, String specialization) { super(name, age); this.specialization = specialization; } void displaySpecialization() { System.out.println("Specialization: " + specialization); } } // Hierarchical Inheritance class Tester extends Person { String testingTool; Tester(String name, int age, String testingTool) { super(name, age); this.testingTool = testingTool; } void displayTestingTool() { System.out.println("Testing Tool: " + testingTool); } } public class InheritanceForms { public static void main(String[] args) { Programmer programmer = new Programmer("Rama", 37, "Java"); Engineer engineer = new Engineer("Mahesh", 21, "Computer Science Engineering"); Tester tester = new Tester("Paani", 22, "Selenium"); programmer.displayDetails(); programmer.displayProgrammingDetails(); System.out.println(); engineer.displayDetails(); engineer.displaySpecialization(); System.out.println(); tester.displayDetails(); tester.displayTestingTool(); } }
Output:
D:\>java InheritanceForms
Name: Rama
Age: 37
Programming Language: Java
Name: Mahesh
Age: 21
Specialization: Computer Science Engineering
Name: Paani
Age: 22
Testing Tool: Selenium