Imagine we have a general Animal class. Animals can have common properties such as a name, and they can perform common actions such as eating.
Now suppose we want to create a Dog class. A dog is an animal, so it would be useful if the Dog class could reuse the properties and methods of the Animal class instead of writing them all again.
This is where inheritance comes in.
Inheritance allows one class to inherit properties and methods from another class.
The class that provides the properties and methods is called the parent class or superclass, while the class that inherits them is called the child class or subclass.
Creating Inheritance
In Java, we use the extends keyword to create inheritance.
For example:
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal {
}Here, Animal is the parent class and Dog is the child class.
Because Dog extends Animal, the Dog class can use the eat() method.
class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
}
}Output:
Animal is eating.We didn't write the eat() method inside Dog. The Dog object inherited it from Animal.
Why Do We Need Inheritance?
The main purpose of inheritance is code reuse.
Imagine we have several types of animals:
Animal
├── Dog
├── Cat
└── HorseAll of these animals may have some common behavior, such as eating and sleeping.
Instead of writing the same methods in every class, we can put the common functionality in Animal:
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
void sleep() {
System.out.println("Animal is sleeping.");
}
}Then:
class Dog extends Animal {
}
class Cat extends Animal {
}Now both Dog and Cat automatically get the eat() and sleep() methods.
Dog dog = new Dog();
dog.eat();
dog.sleep();
Cat cat = new Cat();
cat.eat();
cat.sleep();This avoids unnecessary repeated code.
Adding New Features to the Child Class
A child class doesn't only inherit things from the parent. It can also have its own properties and methods.
For example:
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking.");
}
}Now a Dog object can use both:
Dog dog = new Dog();
dog.eat();
dog.bark();Output:
Animal is eating.
Dog is barking.eat() came from the parent class, while bark() belongs specifically to Dog.
Inherited Variables
Inheritance also works with variables.
class Animal {
String name = "Animal";
}
class Dog extends Animal {
}Now:
Dog dog = new Dog();
System.out.println(dog.name);Output:
AnimalThe Dog object can access the inherited name variable.
Method Overriding
Sometimes a child class inherits a method but wants to provide its own version of that method.
For example:
class Animal {
void sound() {
System.out.println("Animal makes a sound.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks.");
}
}Now:
Dog dog = new Dog();
dog.sound();Output:
Dog barks.The Dog class has replaced the inherited implementation of sound() with its own version.
This is called method overriding.
The @Override annotation tells Java that we intend to override a method from the parent class.
Parent and Child Relationship
Inheritance represents an "is-a" relationship.
For example:
Dog is an Animal
Cat is an Animal
Car is a Vehicle
Student is a PersonSo it makes sense to write:
class Dog extends Animal {
}But it wouldn't make sense to write:
class Engine extends Car {
}if an engine is not a type of car.
Understanding this relationship helps you decide when inheritance is appropriate.
Using super
Java provides the super keyword to refer to the parent class.
For example:
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal {
void show() {
super.eat();
System.out.println("Dog is running.");
}
}Here:
super.eat();calls the eat() method from the parent class.
We'll explore super in more detail separately.
A Complete Example
Let's put the basic ideas together:
class Animal {
String name;
void eat() {
System.out.println(name + " is eating.");
}
}
class Dog extends Animal {
void bark() {
System.out.println(name + " is barking.");
}
}
class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "Buddy";
dog.eat();
dog.bark();
}
}Output:
Buddy is eating.
Buddy is barking.The Dog class inherited the name variable and eat() method from Animal, while bark() was defined inside Dog.
Types of Inheritance in Java
Java supports several inheritance structures through classes, such as:
Single Inheritance
A → B
Multilevel Inheritance
A → B → C
Hierarchical Inheritance
A
/ \
B CJava does not support multiple inheritance through classes. In other words, a class cannot directly extend two classes at the same time.
For example, this is not allowed:
class Dog extends Animal, Pet { // Error
}Java uses interfaces to provide a way for a class to implement multiple types of behavior.
The Main Idea
Inheritance allows a child class to reuse and extend the functionality of a parent class.
The basic syntax is:
class Child extends Parent {
}So remember:
Parent class → provides common functionality
Child class → inherits and can add or modify functionality
Inheritance is one of the core concepts of Object-Oriented Programming, and it becomes especially useful when several classes share common properties or behavior.