Inheritance is one of the four fundamental pillars of Object-Oriented Programming (OOP) in Java. It allows one class to inherit the properties (variables) and behaviors (methods) of another class, promoting code reusability, maintainability, and hierarchical relationships.
Using inheritance, developers can avoid writing the same code repeatedly by reusing existing classes.
Java supports the IS-A relationship through inheritance.
For example:
- A Car is a Vehicle.
- A Dog is an Animal.
- A Student is a Person.
In Java, inheritance is implemented using the extends keyword.
Example:
class Animal {
void eat() {
System.out.println("Animal is Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is Barking");
}
}
Here:
Animal→ Parent Class (Superclass)Dog→ Child Class (Subclass)
The Dog class automatically inherits the eat() method from the Animal class. Java Inheritance practice questions with solutions help to understand the concepts.
Types of Inheritance in Java
Java supports the following inheritance types:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
Java does not support Multiple Inheritance using classes to avoid ambiguity (Diamond Problem). However, it supports multiple inheritance through interfaces.
Inheritance is widely used in:
- Banking Applications
- Hospital Management Systems
- Android Development
- Enterprise Applications
- Spring Boot Projects
- Game Development
- Desktop Software
In this chapter, you’ll solve practical Java inheritance programs frequently asked in interviews and coding assessments.
1. Java Program to Demonstrate Single Inheritance
Problem Statement
Write a Java program to demonstrate Single Inheritance where a child class inherits methods from its parent class.
Java Solution
class Animal {
void eat() {
System.out.println("Animal is Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is Barking");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
}
}
Sample Output
Animal is Eating
Dog is Barking
Explanation
The Dog class inherits the eat() method from the Animal class using the extends keyword.
As a result, the Dog object can access both:
- Parent Class Method
- Child Class Method
Concepts Covered
- Single Inheritance
- Parent Class
- Child Class
- extends Keyword
2. Java Program to Demonstrate Multilevel Inheritance
Problem Statement
Write a Java program to demonstrate Multilevel Inheritance.
Java Solution
class Animal {
void eat() {
System.out.println("Animal is Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is Barking");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Puppy is Weeping");
}
}
public class Main {
public static void main(String[] args) {
Puppy puppy = new Puppy();
puppy.eat();
puppy.bark();
puppy.weep();
}
}
Sample Output
Animal is Eating
Dog is Barking
Puppy is Weeping
Explanation
Inheritance occurs across multiple levels.
Animal
↓
Dog
↓
Puppy
The Puppy class inherits methods from both Dog and Animal.
Concepts Covered
- Multilevel Inheritance
- Parent Class
- Child Class
- Inherited Methods
3. Java Program to Demonstrate Hierarchical Inheritance
Problem Statement
Write a Java program to demonstrate Hierarchical Inheritance.
Java Solution
class Animal {
void eat() {
System.out.println("Animal is Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is Barking");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat is Meowing");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
dog.eat();
dog.bark();
System.out.println();
cat.eat();
cat.meow();
}
}
Sample Output
Animal is Eating
Dog is Barking
Animal is Eating
Cat is Meowing
Explanation
Both Dog and Cat inherit from the same parent class Animal.
Animal
/ \
Dog Cat
Each child class has access to the parent class methods while also defining its own unique behavior.
Concepts Covered
- Hierarchical Inheritance
- Parent Class
- Multiple Child Classes
- Code Reusability
4. Java Program to Use the super Keyword
Problem Statement
Write a Java program to demonstrate the use of the super keyword for accessing a parent class method.
Java Solution
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
void display() {
super.sound();
sound();
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.display();
}
}
Sample Output
Animal makes a sound
Dog barks
Explanation
The super keyword refers to the immediate parent class.
In this example:
super.sound()calls the parent class method.sound()calls the child class method.
This is useful when the child class overrides a parent method but still needs to access the original implementation.
Concepts Covered
superKeyword- Method Overriding
- Parent Class Method
- Child Class Method
5. Java Program to Call Parent Class Constructor Using super()
Problem Statement
Write a Java program to demonstrate how the super() constructor is used to call the parent class constructor.
Java Solution
class Animal {
Animal() {
System.out.println("Animal Constructor Called");
}
}
class Dog extends Animal {
Dog() {
super();
System.out.println("Dog Constructor Called");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
}
}
Sample Output
Animal Constructor Called
Dog Constructor Called
Explanation
The super() statement calls the constructor of the parent class before executing the child class constructor.
If super() is not written explicitly, Java automatically inserts it as the first statement of the child constructor (provided the parent has a no-argument constructor).
This ensures that the parent object is initialized before the child object.
Concepts Covered
super()Constructor- Constructor Chaining
- Parent Constructor
- Child Constructor
6. Java Program to Demonstrate Method Overriding
Problem Statement
Write a Java program to demonstrate Method Overriding, where a child class provides its own implementation of a parent class method.
Java Solution
class Animal {
void sound() {
System.out.println("Animals make different sounds.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog says: Bark Bark");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}
Sample Output
Dog says: Bark Bark
Explanation
Method overriding occurs when a child class defines a method with the same name, return type, and parameters as the parent class.
When the overridden method is called, the child class version executes.
The @Override annotation tells the compiler that the method is intentionally overriding a parent method.
Concepts Covered
- Method Overriding
- Inheritance
- Runtime Polymorphism
@OverrideAnnotation
7. Java Program to Access Parent Class Variables
Problem Statement
Write a Java program to access variables inherited from a parent class.
Java Solution
class Person {
String name = "Rahul Kumar";
}
class Student extends Person {
int rollNumber = 101;
void display() {
System.out.println("Name : " + name);
System.out.println("Roll Number : " + rollNumber);
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.display();
}
}
Sample Output
Name : Rahul Kumar
Roll Number : 101
Explanation
The child class automatically inherits public and protected variables from the parent class.
Here, the Student class accesses the inherited name variable directly without redefining it.
Concepts Covered
- Parent Class Variables
- Child Class
- Variable Inheritance
- Object-Oriented Programming
8. Java Program to Demonstrate Constructor Chaining
Problem Statement
Write a Java program to demonstrate Constructor Chaining using inheritance.
Java Solution
class Vehicle {
Vehicle() {
System.out.println("Vehicle Constructor");
}
}
class Car extends Vehicle {
Car() {
System.out.println("Car Constructor");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
}
}
Sample Output
Vehicle Constructor
Car Constructor
Explanation
Whenever a child object is created, Java automatically calls the parent constructor first.
This process is called Constructor Chaining.
Execution order:
- Parent Constructor
- Child Constructor
Concepts Covered
- Constructor Chaining
- Parent Constructor
- Child Constructor
- Inheritance
9. Java Program to Create a Vehicle and Car Inheritance Example
Problem Statement
Write a Java program to create a Vehicle class and inherit it into a Car class.
Java Solution
class Vehicle {
void startEngine() {
System.out.println("Vehicle Engine Started");
}
}
class Car extends Vehicle {
void drive() {
System.out.println("Car is Moving");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.startEngine();
car.drive();
}
}
Sample Output
Vehicle Engine Started
Car is Moving
Explanation
The Car class inherits the startEngine() method from the Vehicle class.
The child object can call both:
- Parent Method
- Child Method
This demonstrates code reuse using inheritance.
Concepts Covered
- Inheritance
- Vehicle Example
- Parent Class
- Child Class
10. Java Program to Create a Person and Student Inheritance Example
Problem Statement
Write a Java program where a Student class inherits the properties and methods of a Person class.
Java Solution
class Person {
void displayPerson() {
System.out.println("Person Information");
}
}
class Student extends Person {
void displayStudent() {
System.out.println("Student Information");
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.displayPerson();
student.displayStudent();
}
}
Sample Output
Person Information
Student Information
Explanation
The Student class inherits the displayPerson() method from the Person class while also defining its own displayStudent() method.
This demonstrates a simple IS-A relationship:
Person
↓
Student
Concepts Covered
- Parent Class
- Child Class
- Inheritance
- IS-A Relationship
11. Java Program to Demonstrate Hierarchical Inheritance with Employees
Problem Statement
Write a Java program to demonstrate Hierarchical Inheritance, where multiple child classes inherit from the same parent class.
Java Solution
class Employee {
void companyName() {
System.out.println("Company : ABC Technologies");
}
}
class Developer extends Employee {
void writeCode() {
System.out.println("Developer writes Java code.");
}
}
class Tester extends Employee {
void testSoftware() {
System.out.println("Tester performs software testing.");
}
}
public class Main {
public static void main(String[] args) {
Developer developer = new Developer();
Tester tester = new Tester();
developer.companyName();
developer.writeCode();
System.out.println();
tester.companyName();
tester.testSoftware();
}
}
Sample Output
Company : ABC Technologies
Developer writes Java code.
Company : ABC Technologies
Tester performs software testing.
Explanation
Both Developer and Tester inherit the companyName() method from the Employee class.
Each child class has its own additional method.
This demonstrates Hierarchical Inheritance.
Concepts Covered
- Hierarchical Inheritance
- Parent Class
- Multiple Child Classes
- Code Reusability
12. Java Program to Demonstrate Multilevel Inheritance with Animals
Problem Statement
Write a Java program to demonstrate Multilevel Inheritance using animal classes.
Java Solution
class Animal {
void eat() {
System.out.println("Animal Eats Food");
}
}
class Mammal extends Animal {
void walk() {
System.out.println("Mammal Walks");
}
}
class Dog extends Mammal {
void bark() {
System.out.println("Dog Barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.walk();
dog.bark();
}
}
Sample Output
Animal Eats Food
Mammal Walks
Dog Barks
Explanation
Inheritance flows across three levels.
Animal
↓
Mammal
↓
Dog
The Dog class inherits methods from both Animal and Mammal.
Concepts Covered
- Multilevel Inheritance
- Parent Class
- Child Class
- Method Inheritance
13. Java Program to Demonstrate super Keyword for Variables
Problem Statement
Write a Java program to demonstrate the use of the super keyword for accessing parent class variables.
Java Solution
class Animal {
String type = "Animal";
}
class Dog extends Animal {
String type = "Dog";
void display() {
System.out.println("Child Variable : " + type);
System.out.println("Parent Variable : " + super.type);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.display();
}
}
Sample Output
Child Variable : Dog
Parent Variable : Animal
Explanation
The child class has its own variable named type.
Using:
super.type
allows access to the parent class variable.
Concepts Covered
- super Keyword
- Parent Variables
- Child Variables
- Variable Hiding
14. Java Program to Demonstrate Dynamic Method Dispatch
Problem Statement
Write a Java program to demonstrate Dynamic Method Dispatch.
Java Solution
class Animal {
void sound() {
System.out.println("Animal Sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog Barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound();
}
}
Sample Output
Dog Barks
Explanation
Although the reference variable is of type Animal, it points to a Dog object.
At runtime, Java executes the overridden method of the actual object.
This is known as Dynamic Method Dispatch or Runtime Polymorphism.
Concepts Covered
- Dynamic Method Dispatch
- Runtime Polymorphism
- Method Overriding
- Parent Reference
15. Java Program to Create a Real-World Banking Inheritance Example
Problem Statement
Write a Java program to create a simple banking inheritance example where a SavingsAccount inherits from a BankAccount.
Java Solution
class BankAccount {
void accountType() {
System.out.println("Bank Account");
}
}
class SavingsAccount extends BankAccount {
void interestRate() {
System.out.println("Interest Rate : 6.5%");
}
}
public class Main {
public static void main(String[] args) {
SavingsAccount account = new SavingsAccount();
account.accountType();
account.interestRate();
}
}
Sample Output
Bank Account
Interest Rate : 6.5%
Explanation
The SavingsAccount class inherits the common functionality of the BankAccount class while adding its own specific feature (interest rate).
This is a practical example of inheritance in banking software.
Concepts Covered
- Real-World Inheritance
- Banking Example
- Parent Class
- Child Class
- Code Reusability
Chapter Summary
In this chapter, you learned one of the most powerful concepts of Object-Oriented Programming—Inheritance. Inheritance allows a child class to reuse the properties and methods of a parent class, reducing code duplication and making applications more modular and maintainable.
Throughout this chapter, you practiced various inheritance programs, including:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Using the
extendskeyword - Using the
superkeyword - Calling parent class constructors with
super() - Accessing parent class variables
- Method Overriding
- Dynamic Method Dispatch (Runtime Polymorphism)
- Real-world examples such as Vehicle-Car, Person-Student, Employee-Developer, and Bank Account-Savings Account
Inheritance forms the base for advanced Java concepts such as Polymorphism, Abstract Classes, Interfaces, Method Overriding, and Framework Development.
Understanding inheritance is essential for Java interviews and enterprise application development because almost every large Java project uses inheritance to organize and reuse code efficiently.
Key Takeaways
- Inheritance allows one class to reuse another class’s properties and methods.
- The
extendskeyword is used to implement inheritance in Java. - The parent class is also called the Superclass.
- The child class is also called the Subclass.
- Java supports:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Java does not support multiple inheritance using classes.
- The
superkeyword accesses parent class methods, variables, and constructors. - Constructor chaining automatically initializes parent objects before child objects.
- Method Overriding enables Runtime Polymorphism.
- Inheritance improves code reusability, maintainability, and scalability.
Frequently Asked Questions (FAQs)
1. What is inheritance in Java?
Inheritance is an Object-Oriented Programming feature that allows one class to acquire the properties and methods of another class.
Example:
class Dog extends Animal
2. Which keyword is used for inheritance?
Java uses the extends keyword.
Example:
class Student extends Person
3. What is the difference between a parent class and a child class?
| Parent Class | Child Class |
|---|---|
| Superclass | Subclass |
| Provides common properties | Inherits and extends them |
| Base class | Derived class |
4. What is the super keyword?
The super keyword refers to the immediate parent class.
It can be used to:
- Access parent variables
- Access parent methods
- Call parent constructors
Example:
super.display();
5. What types of inheritance does Java support?
Java supports:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
Java does not support multiple inheritance using classes because it can lead to ambiguity (Diamond Problem).
6. What is method overriding?
Method overriding occurs when a child class provides its own implementation of a method that already exists in the parent class.
It enables Runtime Polymorphism.
7. What is Dynamic Method Dispatch?
Dynamic Method Dispatch is Java’s mechanism for resolving overridden method calls at runtime based on the actual object type rather than the reference type.
Example:
Animal animal = new Dog();
animal.sound();
The Dog class’s sound() method is executed.
8. Why is inheritance important?
Inheritance helps developers:
- Reuse code
- Reduce duplication
- Improve maintainability
- Build scalable applications
- Model real-world relationships
- Support polymorphism
- Organize large projects efficiently
It is widely used in enterprise software, Android apps, banking systems, and web applications.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
