The Four Pillars of Java - Part 4 - Inheritance

Inheritance is a fundamental concept in Java and object-oriented programming (OOP) in general. It allows you to create a new class by inheriting properties and behaviors (fields and methods) from an existing class. In Java, you can achieve inheritance using the `extends` keyword.


Here are some key points and examples of inheritance in Java:

1. Base Class (Parent Class or Superclass):
   - The class whose properties and behaviors are inherited is known as the base class, parent class, or superclass.
   - In Java, you can create a base class by defining a regular class.

   class Animal {
       String name;
       
       void eat() {
           System.out.println(name + " is eating.");
       }
   }
   ```

2. Derived Class (Child Class or Subclass):
   - The class that inherits properties and behaviors from another class is called the derived class, child class, or subclass.
   - In Java, you indicate inheritance using the `extends` keyword.


   class Dog extends Animal {
       String breed;
       
       void bark() {
           System.out.println(name + " is barking.");
       }
   }
   ```

   In the example above, `Dog` is a subclass of `Animal`. It inherits the `name` field and the `eat()` method from the `Animal` class and adds its own field `breed` and method `bark()`.

3. Constructor Inheritance:
   - Constructors are not inherited by subclasses, but a subclass can call a constructor of the superclass using the `super()` keyword.
   

   class Dog extends Animal {
       String breed;
       
       Dog(String name, String breed) {
           super(); // Call the superclass constructor (optional if no-argument constructor exists in the superclass)
           this.name = name;
           this.breed = breed;
       }
       
       void bark() {
           System.out.println(name + " is barking.");
       }
   }


4. **Method Overriding:**
   - A subclass can provide its own implementation of a method that is already defined in the superclass. This is called method overriding.
  
   class Cat extends Animal {
       void eat() {
           System.out.println(name + " is eating quietly."); // Override the eat() method
       }
   }




In this example, the `Cat` class overrides the `eat()` method defined in the `Animal` class to provide a different behavior.

5. Access Modifiers and Inheritance:
   - In Java, the access modifier of a field or method in the superclass determines whether it can be accessed or overridden in the subclass.
   - Fields and methods with `private` access are not inherited.
   - Fields and methods with `default`, `protected`, or `public` access can be inherited and accessed in subclasses, with some restrictions based on the access modifier.

Inheritance allows you to create a hierarchy of classes, with more specialized subclasses inheriting and extending the functionality of more general superclasses. It promotes code reusability and the organization of code into logical structures. However, it should be used judiciously to ensure that the resulting class hierarchy is easy to understand and maintain.

Comments