Inheritance is another key concept in object-oriented programming (OOPs) that allows a class to inherit properties and behavior from another class. In Java, a class can inherit methods and attributes from another class, known as the superclass or parent class. The class which inherits methods and attributes from the superclass is called the subclass or child class.
Single Inheritance
In single inheritance, a subclass inherits from only one superclass.
Sample Program:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
public class SingleInheritanceSample{
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
}
}
Multilevel Inheritance
In multilevel inheritance, a subclass inherits from a superclass, and another subclass inherits from the first subclass. This forms a chain of inheritance
Sample Program:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
class Labrador extends Dog {
void play() {
System.out.println("Labrador is playing");
}
}
public class MultilevelInheritanceSample{
public static void main(String[] args) {
Labrador labrador = new Labrador();
labrador.eat();
labrador.bark();
labrador.play();
}
}
Hierarchical Inheritance
In hierarchical inheritance, multiple subclasses inherit from a single superclass. This creates a hierarchy of classes.
Sample Program:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing");
}
}
public class HierarchicalInheritanceSample {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
dog.eat();
dog.bark();
cat.eat();
cat.meow();
}
}
Multiple Inheritance (by implementing the interfaces)
Multiple inheritance is not supported for classes due to the “diamond problem” where conflicts can arise if a class inherits from multiple classes that have the same method or attribute. However, Java allows multiple inheritance through interfaces, where a class can implement multiple interfaces.
Sample Program:
package com.java.how2injava.inheritance;
interface Animal {
void eat();
}
interface Pet {
void play();
}
class Dog implements Animal, Pet {
@Override
public void eat() {
System.out.println("Dog is eating");
}
@Override
public void play() {
System.out.println("Dog is playing");
}
}
public class MultipleInheritanceSample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.play();
}
}
In the program above, the Dog
class implements both the Animal
and Pet
interfaces, achieving a form of multiple inheritance through interfaces in Java.
Hybrid Inheritance
A combination of two or more types of inheritance called Hybrid Inheritance. It can be achieved by combining any of the types of inheritance mentioned earlier (single, multilevel, hierarchical, and multiple inheritance through interfaces).
Sample Program:
interface Animal {
void eat();
}
class Mammal {
void giveBirth() {
System.out.println("Mammal is giving birth");
}
}
class Dog extends Mammal implements Animal {
@Override
public void eat() {
System.out.println("Dog is eating");
}
void bark() {
System.out.println("Dog is barking");
}
}
public class HybridSample{
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
dog.giveBirth();
}
}
In the above program, the Dog
class combines single inheritance (from Mammal
class), multiple inheritance through interfaces (from Animal
interface), and hierarchical inheritance (since Mammal
class is a superclass for Dog
class).
Advantages of Inheritance
- Code reusability: Inheritance promotes code reusability by allowing subclasses to inherit common attributes and behaviors from a superclass. This reduces code duplication and makes it easier to maintain and update the code.
- Modularity: Inheritance helps in creating a modular and organized code structure by allowing you to define common attributes and behaviors in a superclass and then extend or override them in subclasses. This promotes a more organized and structured codebase.
- Polymorphism: Inheritance enables polymorphism, which allows objects of different classes to be treated as objects of a common superclass. This promotes flexibility and allows for more generic and flexible code.
- Extensibility: Inheritance allows you to extend the functionality of existing classes by adding new features or modifying existing ones in subclasses. This makes it easier to adapt your code to changing requirements without modifying the existing code.
- Encapsulation: Inheritance promotes encapsulation by allowing you to hide the implementation details of a superclass and only expose the necessary attributes and methods to subclasses. This helps in maintaining data integrity and reducing the risk of accidental modification.
- Hierarchical structure: Inheritance allows you to create a hierarchical structure of classes, where subclasses can further extend the functionality of their super classes. This helps in organizing and structuring the code in a logical and hierarchical manner.
Conclusion
Inheritance is a powerful feature in Java that promotes code reusability, modularity, polymorphism, extensibility, encapsulation, and hierarchical structure. By allowing classes to inherit properties and behaviors from other classes, inheritance helps in creating more organized, flexible, and maintainable code.
Sample programs on Inheritance available on git