Inheritance in Java

In Java, inheritance is a fundamental object-oriented programming concept that allows classes to inherit properties (fields) and behaviors (methods) from other classes. It is a mechanism where a new class, called a subclass or derived class or child class or extended class, can inherit the characteristics of an existing class, known as the superclass or base class or parent class. In Java, every class is a subclass and descendants of Object class defined in the java.lang package.

Inheritance is represented using the keyword "extends" in Java. The subclass is declared with the "extends" keyword followed by the name of the superclass. This establishes an "is-a" relationship between the subclass and the superclass.

Here's an example to demonstrate the using of inheritance in Java:

// Superclass
class Animal {

    protected String name;
    
    public Animal(String name) {
        this.name = name;
    }
    
    public void eat() {
        System.out.println(name + " is eating.");
    }
}

// Subclass inheriting from Animal
class Dog extends Animal {

    public Dog(String name) {
        super(name);
    }
    
    public void bark() {
        System.out.println(name + " is barking.");
    }

    public void swim() {
        System.out.println(name + " can swim.");
    }
}

// Main class
public class Main {

    public static void main(String[] args) {
        Dog dog = new Dog("Bruno");
        dog.eat(); // Inherited method from Animal
        dog.bark(); // Method specific to Dog
        dog.swim(); // Method specific to Dog
    }
}

The output of the above code is as follows:

Bruno is eating.
Bruno is barking.
Bruno can swim.

In this example, the "Animal" class is the superclass, and the "Dog" class is the subclass. The "Dog" class extends the "Animal" class using the "extends" keyword, which means that it inherits all the fields and methods from the "Animal" class.

The "Dog" class adds two new methods called "bark()" and "swim()" that is specific to dogs. It can access the inherited field "name" and the inherited method "eat" from the superclass.

In the main method, we create an instance of the "Dog" class and call the inherited "eat" method from the "Animal" class and the "bark" and "swim" methods specific to the "Dog" class.


When to Use Inheritance?

Inheritance should be used when you want to acquire all members(fields and methods) of one class in another class. Inheritance allows code reuse, promotes code organization, and supports the concept of polymorphism, where objects of different subclasses can be treated as objects of their superclass. It is a powerful feature in Java that enables building hierarchical relationships between classes and promotes modularity and extensibility in object-oriented programming. 

Constructors are not considered as members of a super class and therefore, they are not inherited in a subclass. However, constructors of a super class can be invoked from its subclasses.

A list of important things to remember while using inheritance:

  • All fields and methods declared as public and protected are inherited in a subclass from its super class, even if the subclasses are in different packages.
  • The inherited field can be used directly or with modifications in a subclass.
  •  A new instance method can be created in a subclass that has exactly the same method's name, return-type and arguments as in the parent class. In other words, it is overriding of methods of the superclass in its subclass.
  • Constructors of the superclass can be invoked from a subclass's constructor using super keyword.
  • A new static method can be created in a subclass that has exactly the same method's name, return-type and arguments as in the superclass. In other words, it is hiding of methods of the superclass in its subclass.
  • New fields and methods, which are not in the superclass can be created in its subclasses.
  • A new field with the same name and return-type as the one in the superclass, can be created in its subclass. This will hide the field of the superclass and thus not recommended.
  • A subclass cannot inherit a private method of a superclass. However, if a superclass has public or protected methods that provides access to its private fields then these methods can be used by its subclasses to access those private fields of the superclass.
  • A nested class can access all private fields and methods of the enclosing class. This makes private fields and methods of the superclass indirectly accessible from the inherited public or protected nested class in a subclass.