Introduction
Inheritance is an object-oriented programming feature that allows one class to acquire the accessible attributes and methods of another class.
Inheritance creates a relationship between:
Super class: The super class (also known as the parent class) is the class that provides common properties (attributes) and behaviors (methods) that are shared by one or more subclasses. It serves as a template or blueprint from which other classes (subclasses) can inherit.
Sub class: A sub class (also known as a child class) is a class that inherits from a super class. The sub class can reuse, extend, or override the attributes and methods of the parent class to specialize or modify the inherited functionality.
Java uses the extends keyword to create inheritance between classes.
Features of Inheritance
Reuses existing code
Reduces code duplication
Creates parent-child relationships between classes
Allows child classes to add new features
Supports method overriding and polymorphism
Makes programs easier to organize and maintain
Inheritance Example
// Parent class or superclassclass Animal { // Protected attribute is accessible in child classes protected String name = "Dog"; // Method that can be inherited by child classes public void eat() { System.out.println(name + " is eating."); }}// Child class inherits from Animal using extendsclass Dog extends Animal { // Method defined specifically in the Dog class public void bark() { System.out.println(name + " is barking."); }}public class Main { public static void main(String[] args) { // Create an object of the child class Dog dog = new Dog(); // Access the inherited method from Animal dog.eat(); // Output : Dog is eating. // Access the method defined in Dog dog.bark(); // Output : Dog is barking. }}In this example, Animal is the superclass and Dog is the subclass. The Dog class inherits the name attribute and eat() method from Animal.
Inheritance
Types of Inheritance:
In Java, there are three major types of inheritance:
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
1. Single Inheritance
In Single Inheritance, a child class inherits from one parent class. This is the simplest and most common form of inheritance. Consider the following code snippet:
import java.util.*;// Parent classclass Animal { // Method to represent the eating behavior of an animal void eat() { System.out.println("This animal eats food."); }}// Child class inheriting from the Animal classclass Dog extends Animal { // Method specific to the Dog class to represent barking behavior void bark() { System.out.println("This dog barks."); }}// Main class to execute the programclass Main { public static void main(String[] args) { // Create an object of the Dog class Dog dog = new Dog(); // Call the eat method inherited from the Animal class dog.eat(); // Output: This animal eats food. // Call the bark method defined in the Dog class dog.bark(); // Output: This dog barks. }}Here, the Dog class inherits from only one parent class, Animal, using the extends keyword, so this is an example of single inheritance.
Single Inheritance
2. Multilevel Inheritance
In Multilevel Inheritance, a class derives from a child class, creating a chain of inheritance. Here, the child class of one level becomes the parent class for the next level. Consider the code snippet below:
import java.util.*;// Parent class representing general animalsclass Animal { // Method to define the eating behavior of animals void eat() { System.out.println("This animal eats food."); }}// Intermediate class representing mammals, inheriting from Animalclass Mammal extends Animal { // Method to define the walking behavior of mammals void walk() { System.out.println("This mammal walks."); }}// Subclass representing dogs, inheriting from Mammalclass Dog extends Mammal { // Method to define the barking behavior specific to dogs void bark() { System.out.println("This dog barks."); }}// Main class to demonstrate multilevel inheritanceclass Main { public static void main(String[] args) { // Create an object of the Dog class Dog dog = new Dog(); // Call the eat method inherited from the Animal class dog.eat(); // Output: This animal eats food. // Call the walk method inherited from the Mammal class dog.walk(); // Output: This mammal walks. // Call the bark method defined in the Dog class dog.bark(); // Output: This dog barks. }}Here, the Dog class inherits from Mammal, which itself inherits from Animal, creating a chain of inheritance across multiple levels, so this is an example of multilevel inheritance.
Multilevel Inheritance
3. Hierarchical Inheritance
In Hierarchical Inheritance, multiple child classes inherit from a single parent class. Consider the code snippet below:
import java.util.*;// Parent class representing general animalsclass Animal { // Method to define the eating behavior common to all animals void eat() { System.out.println("This animal eats food."); }}// Subclass representing dogs, inheriting from Animalclass Dog extends Animal { // Method to define the barking behavior specific to dogs void bark() { System.out.println("This dog barks."); }}// Subclass representing cats, inheriting from Animalclass Cat extends Animal { // Method to define the meowing behavior specific to cats void meow() { System.out.println("This cat meows."); }}// Main class to demonstrate hierarchical inheritanceclass Main { public static void main(String[] args) { // Create an object of the Dog class Dog dog = new Dog(); // Create an object of the Cat class Cat cat = new Cat(); // Call the eat method inherited from the Animal class using the Dog object dog.eat(); // Output: This animal eats food. // Call the bark method specific to the Dog class dog.bark(); // Output: This dog barks. // Call the eat method inherited from the Animal class using the Cat object cat.eat(); // Output: This animal eats food. // Call the meow method specific to the Cat class cat.meow(); // Output: This cat meows. }}Here, both the Dog and Cat classes inherit from the same parent class, Animal, forming a one-parent-to-multiple-child relationship, so this is an example of hierarchical inheritance.
Hierarchical Inheritance
Advantages of Inheritance
Code reusability: Child classes can reuse parent class members.
Less duplication: Common code can be written once in the parent class.
Easy extension: Child classes can add new attributes and methods.
Better organization: Related classes can be arranged in a hierarchy.
Maintainability: Common changes can be made in the parent class.
Summary
Inheritance in Java allows one class to acquire the accessible attributes and methods of another class using the extends keyword. The parent class provides common functionality, while the child class can reuse, extend, or override that functionality.
Java supports single, multilevel, and hierarchical inheritance through classes. Inheritance improves code reusability, reduces duplication, and helps organize related classes into clear parent-child relationships.
Be the first to add a comment.