Zone Of Makos

Menu icon

Inheritance and Polymorphism

In object-oriented programming, inheritance and polymorphism are powerful concepts that allow for code reuse, modularity, and flexibility. In this section, we will explore how inheritance and polymorphism work in Java and how you can leverage them in your programs.

Understanding Inheritance

Inheritance is a mechanism that allows a class to inherit properties and behaviors from another class, known as the superclass or base class. The class inheriting these properties and behaviors is called the subclass or derived class. With inheritance, you can create a hierarchy of classes, where subclasses inherit characteristics from their parent classes.

Extending a Class

// Superclass
class Animal {
  protected String name;

  public Animal(String name) {
    this.name = name;
  }

  public void speak() {
    System.out.println("The animal makes a sound.");
  }
}

// Subclass
class Cat extends Animal {
  public Cat(String name) {
    super(name);
  }

  public void speak() {
    System.out.println("Meow!");
  }
}

// Subclass
class Dog extends Animal {
  public Dog(String name) {
    super(name);
  }

  public void speak() {
    System.out.println("Woof!");
  }
}

In the code snippet above, we have a superclass called `Animal` with a `name` member variable and a `speak()` method. The `Cat` and `Dog` classes extend this superclass using the `extends` keyword. They inherit the `name` property and override the `speak()` method to provide their specific implementation.

Using Inherited Members

Cat cat = new Cat("Whiskers");
Dog dog = new Dog("Buddy");

System.out.println(cat.name); // Output: Whiskers
System.out.println(dog.name); // Output: Buddy

cat.speak(); // Output: Meow!
dog.speak(); // Output: Woof!

In the code snippet above, we create instances of the `Cat` and `Dog` classes and assign them specific names. We can access the inherited `name` property directly from the instances. When we call the `speak()` method, polymorphism comes into play. The JVM determines the correct implementation of the `speak()` method based on the actual type of the object.

Understanding Polymorphism

Polymorphism is the ability of an object to take on many forms. In Java, polymorphism is achieved through method overriding and dynamic method dispatch. It allows you to write code that can work with objects of different classes but still perform the appropriate behavior for each specific object.

Method Overriding

// Superclass
class Shape {
  public void draw() {
    System.out.println("Drawing a shape.");
  }
}

// Subclass
class Circle extends Shape {
  public void draw() {
    System.out.println("Drawing a circle.");
  }
}

// Subclass
class Rectangle extends Shape {
  public void draw() {
    System.out.println("Drawing a rectangle.");
  }
}

In the code snippet above, we have a superclass called `Shape` with a `draw()` method. The `Circle` and `Rectangle` classes override this method to provide their own implementation of drawing. This is known as method overriding.

Dynamic Method Dispatch

Shape shape1 = new Circle();
Shape shape2 = new Rectangle();

shape1.draw(); // Output: Drawing a circle.
shape2.draw(); // Output: Drawing a rectangle.

In the code snippet above, we create instances of the `Circle` and `Rectangle` classes but assign them to variables of type `Shape`, which is their superclass. We can then call the `draw()` method on these variables, which triggers dynamic method dispatch. The JVM determines the actual type of the object at runtime and executes the corresponding overridden method.

Polymorphism and Method Overloading

Polymorphism is not limited to method overriding. It can also be achieved through method overloading. Method overloading allows you to define multiple methods with the same name but different parameters in a class. The JVM determines the appropriate method to call based on the arguments provided.

With a solid understanding of inheritance and polymorphism, you can now design more flexible and modular code in Java. These concepts provide the foundation for creating hierarchies of classes and writing code that can adapt to different scenarios. Keep exploring and experimenting with Java to unlock its full potential!