Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and behaviors (fields and methods) from another class. It promotes code reuse and establishes a natural hierarchical relationship between classes.
extends Keyword:
extends keyword. A subclass uses extends followed by the superclass name.super Keyword:
super keyword refers to the superclass and can be used to access superclass methods and constructors.Single Inheritance:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
Multilevel Inheritance:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
class Labrador extends Dog {
void display() {
System.out.println("This is a Labrador.");
}
}
Hierarchical Inheritance:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
class Cat extends Animal {
void meow() {
System.out.println("The cat meows.");
}
}
Hybrid Inheritance (Through Interfaces):
interface Animal {
void eat();
}
interface Pet {
void play();
}
class Dog implements Animal, Pet {
public void eat() {
System.out.println("This dog eats food.");
}
public void play() {
System.out.println("The dog plays.");
}
}
Here’s a UML diagram representing the types of inheritance in Java:
Single Inheritance:
Animal
|
Dog
Multilevel Inheritance:
Animal
|
Dog
|
Labrador
Hierarchical Inheritance:
Animal
/ \\\\
Dog Cat
Hybrid Inheritance:
Animal Pet
\\\\ /
\\\\ /
\\\\ /
\\\\ /
Dog
Let's illustrate inheritance with a more detailed example:
// Superclass
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void eat() {
System.out.println(name + " eats.");
}
void sleep() {
System.out.println(name + " sleeps.");
}
}
// Subclass
class Dog extends Animal {
String breed;
Dog(String name, String breed) {
super(name); // Calling the superclass constructor
this.breed = breed;
}
void bark() {
System.out.println(name + " barks.");
}
// Overriding the eat method
@Override
void eat() {
System.out.println(name + " eats dog food.");
}
}
// Another Subclass
class Cat extends Animal {
boolean isIndoor;
Cat(String name, boolean isIndoor) {
super(name);
this.isIndoor = isIndoor;
}
void meow() {
System.out.println(name + " meows.");
}
// Overriding the sleep method
@Override
void sleep() {
System.out.println(name + " sleeps on a cushion.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy", "Golden Retriever");
dog.eat(); // Buddy eats dog food.
dog.sleep(); // Buddy sleeps.
dog.bark(); // Buddy barks.
Cat cat = new Cat("Whiskers", true);
cat.eat(); // Whiskers eats.
cat.sleep(); // Whiskers sleeps on a cushion.
cat.meow(); // Whiskers meows.
}
}
In this example:
Animal class is the superclass with common properties and methods.