Inheritance in Java is one of the fundamental concepts in object-oriented programming. It is the mechanism by which a new class can be derived from an existing class.
Table of Contents
What is Inheritance in Java?
Inheritance is a mechanism in Java by which one class can acquire the properties (methods and fields) of another class. The class that inherits properties of another class is called a derived class or subclass, and the class whose properties are inherited is called a base class or superclass.
Types of Inheritance
There are five types of inheritance in Java:
Single Inheritance: In this type of inheritance, a class extends only one base class.
Multi-level Inheritance: In this type of inheritance, a derived class extends a base class, which in turn extends another base class.
Hierarchical Inheritance: In this type of inheritance, multiple derived classes extend a single base class.
Multiple Inheritance: Java does not support multiple inheritances. It is the ability of a class to inherit properties from more than one base class.
Hybrid Inheritance: It is a combination of any two or more types of inheritance.
Using Inheritance in Java
To use inheritance in Java, you simply create an instance of the derived class, and the derived class will have access to all the methods and fields of the base class. Here is an example:
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Rufus", 5, "Labrador");
myDog.speak();
myDog.bark();
}
}
Output

In the above example, the “Main” class creates an instance of the “Dog” class and calls the “speak” and “bark” methods of the “Dog” class.
Base Class in Java
In Java, a base class is defined using the “class” keyword, followed by the class name, and then the class body in curly braces.
public class Animal {
private String name;
private int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void speak() {
System.out.println("The animal speaks.");
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal("Leo", 3);
myAnimal.speak();
}
}
}
Output

Derived Class in Java
In Java, a derived class is defined using the “class” keyword, followed by the class name, the “extends” keyword, and the name of the base class. Here is an example:
public class Dog extends Animal {
private String breed;
public Dog(String name, int age, String breed) {
super(name, age);
this.breed = breed;
}
public void bark() {
System.out.println("The dog barks.");
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Rufus", 5, "Labrador");
myDog.speak();
myDog.bark();
}
}
}
Output

In the above example, the “Dog” class extends the “Animal” class using the “extends” keyword. The constructor of the “Dog” class uses the “super” keyword to call the constructor of the “Animal” class and initialize the “name” and “age” fields.
Conclusion
Inheritance is a powerful tool in Java for creating new classes by extending existing ones. Understanding the different types of inheritance and defining base and derived classes leads to efficient and effective programming. Implementing code examples helps to understand how inheritance works in action.