Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Inheritance and Polymorphism

11. Inheritance and Polymorphism

Inheritance and polymorphism are two fundamental concepts in object-oriented programming (OOP). These concepts allow you to create more organized, modular, and reusable code in Java. In this Core Java tutorial, we'll explore inheritance and polymorphism in detail, providing explanations and examples.

Inheritance:

Inheritance is a mechanism in which one class inherits the properties and behaviors (fields and methods) of another class. The class that inherits from another class is called a subclass or derived class, while the class being inherited from is called the superclass or base class. In Java, inheritance is achieved using the 'extends' keyword.

Syntax of Inheritance:
class Subclass extends Superclass {
    // Subclass-specific fields and methods
}
 
  • 'Subclass': The name of the subclass.
  • 'Superclass': The name of the superclass being inherited from.
Example of Inheritance:
class Animal {
    void eat() {
        System.out.println("Animal is eating.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog is barking.");
    }
}
 

In this example, the 'Dog' class inherits the  'eat'method from the 'Animal'class.

Method Overriding:

Inheritance allows you to override (redefine) methods inherited from the superclass in the subclass. This enables the subclass to provide its own implementation of the method.

Example of Method Overriding:
class Shape {
    void draw() {
        System.out.println("Drawing a shape.");
    }
}

class Circle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a circle.");
    }
}
 

In this example, the 'Circle'class overrides the 'draw' method inherited from the 'Shape' class to provide its own implementation.

Polymorphism:

Polymorphism is a concept that allows objects of different classes to be treated as objects of a common superclass. It enables flexibility in method invocation based on the actual object type. Polymorphism is achieved through method overriding and interfaces in Java.

Example of Polymorphism:
class Shape {
    void draw() {
        System.out.println("Drawing a shape.");
    }
}

class Circle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a circle.");
    }
}

class Square extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a square.");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape shape1 = new Circle();
        Shape shape2 = new Square();

        shape1.draw(); // Calls the draw method of Circle
        shape2.draw(); // Calls the draw method of Square
    }
}
 

In this example, we create objects of the 'Circle' and  'Square'classes and store them in variables of type 'Shape'. When we call the 'draw' method on these variables, Java invokes the appropriate 'draw' method of the actual object type (polymorphism in action).

Inheritance vs. Composition:

Inheritance is a way to create a new class by reusing the properties and behaviors of an existing class, while composition is a way to create a new class by combining objects of other classes. Both have their use cases, and you should choose the one that fits your design needs.

Inheritance:

Pros:
  • Code reuse and extension of existing classes.
  • Easy to understand and implement.
Cons:
  • Can lead to tight coupling between classes.
  • Inflexible when dealing with multiple inheritance

Composition:

Pros:
  • Flexibility in combining different components.
  • Avoids the issues of multiple inheritance.
Cons:
  • May require more code to delegate method calls.
  • May be less intuitive for simple hierarchies.

Conclusion:

Inheritance and polymorphism are powerful concepts in Java that enable code reuse, flexibility, and extensibility. They allow you to create well-organized and maintainable code by modeling real-world relationships and behaviors effectively. Understanding how to use inheritance, method overriding, and polymorphism is essential for building sophisticated Java applications.