Inheritance In Java

Deep Dive into Inheritance Concept in Java
Inheritance is one of the fundamental concepts of Object-Oriented Programming (OOP) in Java. It allows one class (subclass/child class) to inherit the fields (attributes) and methods (behaviors) of another class (superclass/parent class). Inheritance promotes code reusability and establishes a natural hierarchy between classes.

Importance of Inheritance

  1. Code Reusability: Inheritance allows subclasses to reuse the code of the parent class. This reduces code duplication and enhances maintainability.
  2. Method Overriding: Subclasses can provide specific implementations for methods that are already defined in the superclass, enabling polymorphism.
  3. Hierarchy Representation: Inheritance helps in establishing a hierarchical relationship between classes, reflecting real-world relationships.
  4. Extensibility: Existing code can be extended with new features without modifying it, promoting a clean and modular design.
  5. Extends Keyword: In Java, the extends keyword is used to define a subclass.
  6. Protected Access: Members declared as protected can be accessed by subclasses and classes in the same package.
  7. Constructor Chaining: Subclass constructors implicitly call the superclass constructor. The super() keyword is used to explicitly call a superclass constructor.
  8. Single Inheritance: Java supports single inheritance, but multiple inheritance can be achieved through interfaces.
  9. Code Reuse: Inheritance promotes code reuse, allowing a new class to be created with the properties of an existing class.
  10. IS-A Relationship: Inheritance represents an IS-A relationship between the superclass and subclass.

Benefits

  • Reduces Redundancy: Common functionality can be defined in a superclass, reducing code duplication.
  • Enhances Maintainability: Changes in the superclass are automatically inherited by subclasses

Usage of Inheritance

Inheritance is used in scenarios where a class needs to share attributes and behaviors with another class. Common use cases include:

  1. Generalization-Specialization: Creating a general class (e.g., Animal) and more specialized subclasses (e.g., Dog, Cat) that inherit from the general class.
  2. Code Reuse: Reusing existing code by creating a new class that inherits from an existing class.
Code Example
Output
The cat meows.
The cat purrs.

Conclusions

Inheritance of Methods and Properties:

  • Inheritance: Allows child classes to inherit methods and properties from parent classes, promoting code reuse and establishing a natural hierarchy.
  • Whatever methods and properties the parent class (Animal) has, they are by default available to the child class (Cat). This means that with a child class reference, you can call both parent and child class methods.

Method Availability:

  • Methods specific to the child class are not available to the parent class.
  • Whatever methods the child class (Cat) has that are not in the parent class (Animal), cannot be called with a parent class reference. This means you can't call purr using an Animal reference, even if it refers to a Cat object.

Parent Reference Holding Child Object:

  • A parent class reference can be used to hold a child object, but child class reference cannot be used to hold parent class objects. However, by using the parent class reference, you can't call child class-specific methods but can call all methods available in the parent class. This is because the reference type determines what methods can be called.

    Animal myAnimal = new Cat();  // Parent reference to Child object
    myAnimal.purr();  // This line would cause a compile-time error

Method Overriding and Polymorphism:

  • When a child class overrides a parent class method, the method of the child class is called, even if the reference type is the parent class.
  • When a method is overridden in the child class, calling that method on a parent reference pointing to a child object will invoke the child class's overridden method. This demonstrates runtime polymorphism.

    Animal myAnimal = new Cat();  // Parent reference to Child object
    myAnimal.makeSound();  // Calls the overridden method in Cat class

In this example, we demonstrate the concept of using a parent class reference to refer to a child class object.

Output

The dog eats bones.
This animal sleeps.
The dog barks.