JAVA Tutorial



INHERITANCE IN JAVA


Inheritance in Java

Inheritance is one of the fundamental concepts of Object-Oriented Programming (OOP). It allows a new class to inherit properties and behaviors (methods) from an existing class. This promotes code reusability and establishes a relationship between the parent (superclass) and child (subclass) classes.

What is Inheritance?

In Java, inheritance is achieved by using the extends keyword. The subclass inherits fields and methods from the superclass. It can also have additional fields and methods of its own, or override the inherited methods.

Why Use Inheritance?

  • Code Reusability: Inheritance allows you to reuse code from the superclass in the subclass, avoiding duplication.
  • Method Overriding: Subclasses can modify the inherited behavior of methods from the superclass.
  • Establishing Relationships: It allows you to create a hierarchy between classes, indicating that one class is a specialized version of another.

Types of Inheritance in Java

There are different types of inheritance in Java:

  • Single Inheritance: One class inherits from another class.
  • Multilevel Inheritance: A class inherits from another class, which itself is inherited from a superclass.
  • Hierarchical Inheritance: Multiple classes inherit from a single superclass.
  • Multiple Inheritance (through interfaces): Java doesn’t support multiple inheritance of classes, but it can be achieved through interfaces.

Single Inheritance Example

Let’s take a look at a simple example of single inheritance where a Dog class inherits from the Animal class.

// Superclass (Parent class)
class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

// Subclass (Child class)
class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();  // Inherited method
        dog.bark(); // Subclass method
    }
}
  

In this example, the Dog class inherits the eat() method from the Animal class. The Dog class also has its own bark() method.

Method Overriding in Inheritance

Inheritance allows method overriding, where a subclass provides its own implementation of a method that is already defined in the superclass. Here's how it works:

// Superclass (Parent class)
class Animal {
    void sound() {
        System.out.println("Animal makes a sound.");
    }
}

// Subclass (Child class)
class Dog extends Animal {
    // Overriding the sound method
    @Override
    void sound() {
        System.out.println("Dog barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal = new Animal();
        animal.sound();  // Superclass method

        Dog dog = new Dog();
        dog.sound(); // Overridden method
    }
}
  

In this case, the Dog class overrides the sound() method of the Animal class to provide a more specific behavior (dog barking). The @Override annotation is used to indicate that the method in the subclass is overriding a method in the superclass.

Multilevel Inheritance Example

In multilevel inheritance, a class is derived from another class, which itself is derived from another class. Here’s an example:

// Grandparent class
class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

// Parent class (inherits Animal)
class Mammal extends Animal {
    void sleep() {
        System.out.println("This mammal sleeps.");
    }
}

// Child class (inherits Mammal)
class Dog extends Mammal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();   // Inherited from Animal
        dog.sleep(); // Inherited from Mammal
        dog.bark();  // Specific to Dog
    }
}
  

The Dog class inherits from the Mammal class, which in turn inherits from the Animal class. This demonstrates multilevel inheritance in action.

Polymorphism and Inheritance

Polymorphism, a key concept in OOP, often goes hand-in-hand with inheritance. It allows a subclass to implement its own version of a method while still using the parent class's method signature. Polymorphism can be achieved through method overriding or method overloading.

Example of Polymorphism

// Superclass
class Animal {
    void sound() {
        System.out.println("Animal makes a sound.");
    }
}

// Subclass
class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks.");
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Animal();
        myAnimal.sound();  // Animal makes a sound

        Animal myDog = new Dog(); // Polymorphism
        myDog.sound();  // Dog barks
    }
}
  

In this case, the myDog object is of type Animal, but it refers to a Dog object. This demonstrates polymorphism, where the actual method that gets called depends on the object type, not the reference type.

Advantages of Inheritance

  • Code Reusability: Inheritance allows new classes to reuse the code of existing classes without rewriting it.
  • Extensibility: It’s easy to add new functionality to the subclass without changing the existing code.
  • Organized Code: Inheritance establishes a natural hierarchy and makes the code more organized and easier to understand.

Summary

Inheritance is a core feature of Java and enables the creation of new classes based on existing ones. It fosters code reusability and allows for method overriding and polymorphism. It plays a crucial role in organizing and simplifying code in object-oriented programming.

Quick Tip:

Remember that while inheritance promotes code reusability, it’s important to avoid excessive inheritance chains, which can make the code more complex and harder to maintain.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review