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.
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.
There are different types of inheritance in Java:
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.
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.
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, 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.
// 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.
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.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!