Inheritance is one of the four fundamental concepts (Encapsulation, Inheritance, Polymorphism, and Abstraction) in Java OOP. It is the mechanism in Java to allow classes to inherit data and methods from another class. It provides a way to establish hierarchical relationships between classes, promoting code reuse, and improve maintainability.
Key concepts of inheritance
Parent class (Superclass)
The class whose properties and behaviours are inherited is the superclass or parent class. The inheritance in Java is implemented by the extends keyword.
Subclass
The class that inherits from the superclass is called the subclass or child class.
The subclass can use the properties and methods of the superclass and can also have additional properties and methods or override the inherited ones.
Single inheritance
Single inheritance is supported in Java, meaning one class can only inherit from one superclass. However, a class can implement multiple interfaces.
Accessibility
Public and protected fields of a superclass can be directly accessed by its subclasses. (More about access modifier in Access Modifier)
// superclasspublic class Animal { protected String name = name; public Animal(String name) { this name = name; } public void eat() { System.out.println(name + " is eating"); } public void makeSound() { System.out.println("Generic animal sound"); }}// subclasspublic class Dog extends Animal { @Override public void makeSound() { System.out.println("Bark"); }}public class Main { public static void main(String[] args) { Animal myAnimal = new Animal(); Animal myDog = new Dog(); myAnimal.makeSound(); myDog.makeSound(); }}
Generic animal sound
Bark
Super keyword
In Java, when you instantiate a child object, the constructor of the parent class is also invoked. This process ensures the parent class is initialised before the child class. This invocation happens implicitly through a call to the super() to invoke the super class constructor, which is inserted by Java compiler if not explicitly provided by the programmer.
Implicit call
If you do not explicitly call the super() method is the subclass constructor, the Java compiler will automatically call the non-argument constructor of the superclass.
class Parent { public Parent() { System.out.println("Parent constructor called"); }}class Child extends Parent { public Child() { // implicit call to super() System.out.println("Child constructor called"); }}public class Main { public static void main(String[] args) { Child child = new Child(); }}
Parent constructor called
Child constructor called
Explicit call
You can explicitly call the super class constructor by using the super() method. You can supply arguments to this call.
class Parent { public Parent(String message) { System.out.println("Parent constructor called: " + message); }}class Child extends Parent { public Child() { super("Hello from Child"); // explicit call to superclass constructor System.out.println("Child constructor called"); }}public class Main { public static void main(String[] args) { Child child = new Child(); }}
Parent constructor called: Hello from Child
Child constructor called
The Object class
In Java, every class is implicitly a subclass of the java.lang.Object class if it does not explicitly extend another class. When a class in initialised, there is always an implicit call to the Object Class constructor.
Method overriding
Method Overriding is different from Method Overloading
The Method Overloading occurs when a class has more than one method with the same name but different parameters (different type or number of parameters, or both). Overloaded methods can have different return types and access modifiers.
class Utilities { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } public int add(int a, int b, int c) { return a + b + c; }}
Method overriding is where subclass provides a specific implementation of a method that already defined in the superclass.
The method signature must be the same (method name, return type, and parameters)
class Animal { public void makeSound() { System.out.println("Generic animal sound"); }}class Dog extends Animal { @Override public void makeSound() { System.out.println("Bark"); }}public class Main { public static void main(String[] args) { Animal myAnimal = new Animal(); Animal myDog = new Dog(); myAnimal.makeSound(); myDog.makeSound(); }}