🧠 What is Inheritance?
-Inheritance is the mechanism in Java by which one class can acquire the properties (fields) and behaviors (methods) of another class.
-The base class or superclass is the class being inherited from.
-The subclass or child class is the class that inherits from the superclass.
Why use inheritance?
✅ Code reuse – avoid writing the same code again.
✅ Improved organization – logically structure related classes.
✅ Polymorphism – enables dynamic method calls, which makes your code more flexible and extensible.
🛠️ Syntax of Inheritance in Java
1.Java uses the extends keyword to establish an inheritance relationship.
EXAMPLE:
// Superclass
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
// Subclass
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // Inherited from Animal
d.bark(); // Defined in Dog
}
}
1. Single Inheritance
In single inheritance, a subclass inherits from only one superclass. This is the most straightforward and common form of inheritance.
Example:
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // Inherited from Animal
d.bark(); // Defined in Dog
}
}
Output:
Eating...
Barking...
Here, the Dog class inherits the eat() method from the Animal class.
2. Multilevel Inheritance
In multilevel inheritance, a class is derived from another subclass, creating a chain of inheritance.
Example:
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
class Puppy extends Dog {
void play() {
System.out.println("Playing...");
}
}
public class Main {
public static void main(String[] args) {
Puppy p = new Puppy();
p.eat(); // Inherited from Animal
p.bark(); // Inherited from Dog
p.play(); // Defined in Puppy
}
}
*Output:
*
Eating...
Barking...
Playing...
In this case, the Puppy class is a subclass of Dog, which in turn is a subclass of Animal. The Puppy class inherits the methods from both Dog and Animal.
3. Hierarchical Inheritance
In hierarchical inheritance, a single superclass is inherited by multiple subclasses. All subclasses share the same properties and behaviors defined in the superclass.
Example:
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Meowing...");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
Cat c = new Cat();
d.eat(); // Inherited from Animal
d.bark(); // Defined in Dog
c.eat(); // Inherited from Animal
c.meow(); // Defined in Cat
}
}
Output:
Eating...
Barking...
Eating...
Meowing...
In this example, both Dog and Cat inherit from the Animal class, but each has its own additional behavior (like barking and meowing).
4. Multiple Inheritance (via Interfaces) (TDB)
Java does not support multiple inheritance with classes directly, meaning a class cannot inherit from more than one class. However, multiple inheritance can be achieved using interfaces. A class can implement multiple interfaces, which allows it to inherit the behavior of multiple sources.
Example:
interface Animal {
void eat();
}
interface Pet {
void play();
}
class Dog implements Animal, Pet {
public void eat() {
System.out.println("Eating...");
}
public void play() {
System.out.println("Playing...");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // From Animal interface
d.play(); // From Pet interface
}
}
Output:
Eating...
Playing...
In this case, the Dog class implements two interfaces (Animal and Pet), allowing it to inherit behaviors from both.
5. Hybrid Inheritance (Not Supported) (TBD)
Hybrid inheritance is a combination of two or more types of inheritance. However, Java does not support hybrid inheritance with classes because it can lead to ambiguity and complexity. For example, trying to inherit from two classes would cause conflicts in method or field resolution.
While Java does not support this directly, hybrid inheritance can be mimicked using interfaces, as shown in the previous example where a class implements multiple interfaces.
Key Points:
- Single Inheritance: One subclass inherits from one superclass.
- Multilevel Inheritance: A subclass inherits from another subclass.
- Hierarchical Inheritance: Multiple subclasses inherit from a single superclass.
- Multiple Inheritance: Achieved through interfaces, where a class can implement multiple interfaces.
- Hybrid Inheritance: Not supported with classes in Java, but can be mimicked with interfaces.