-
Hajipur, Bihar, 844101
Inheritance is one of the core principles of Object-Oriented Programming (OOP) in Java. It allows one class to acquire the properties and behaviors (fields and methods) of another class. This feature promotes code reuse and helps build relationships between classes in a structured way.
In simple terms, inheritance means creating a new class based on an existing class. The new class (called the subclass or child class) inherits the features of the existing class (called the superclass or parent class).
Inheritance lets you create a class that reuses code from another class without rewriting it. The main idea is that a subclass can use all accessible members (variables and methods) of its superclass.
Java supports single inheritance, meaning a class can inherit from only one superclass. However, a class can implement multiple interfaces (we’ll cover that later).
To declare inheritance, Java uses the keyword extends.
class Parent {
// parent class members
}
class Child extends Parent {
// child class members
}
Here, Child inherits all accessible properties and methods from Parent.
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // inherited method
d.bark(); // child class method
}
}
Output:
This animal eats food.
The dog barks.
Here, Dog automatically inherits the eat() method from Animal. You didn’t have to redefine it.
Code Reusability – Write common functionality once in the parent class and reuse it in child classes.
Method Overriding – Child classes can modify parent methods for their specific behavior.
Organization – Helps build a logical class hierarchy.
Extensibility – New features can be added without modifying existing code.
Although Java supports only single inheritance through classes, it allows other inheritance structures logically.
A single subclass inherits from one superclass.
class Vehicle {
void run() {
System.out.println("Vehicle is running");
}
}
class Car extends Vehicle {
void start() {
System.out.println("Car is starting");
}
}
A class can inherit from a class that already inherits from another.
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Mammal extends Animal {
void walk() {
System.out.println("Walking...");
}
}
class Dog extends Mammal {
void bark() {
System.out.println("Barking...");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.walk();
d.bark();
}
}
Output:
Eating...
Walking...
Barking...
When multiple classes inherit from the same parent class.
class Animal {
void eat() {
System.out.println("Animal eats food");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat meows");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
Here, both Cat and Dog share the eat() method from Animal.
Java doesn’t allow multiple inheritance through classes to avoid ambiguity, but it can be achieved using interfaces.
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
public void methodA() {
System.out.println("Method A");
}
public void methodB() {
System.out.println("Method B");
}
}
The super keyword in Java refers to the immediate parent class. It can be used to:
Access parent class variables or methods.
Call the parent class constructor.
Example:
class Parent {
int age = 50;
}
class Child extends Parent {
int age = 20;
void display() {
System.out.println("Child age: " + age);
System.out.println("Parent age: " + super.age);
}
}
Output:
Child age: 20
Parent age: 50
A subclass can redefine a method from its parent class using the same name and parameters.
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
Output:
Dog barks
This is called runtime polymorphism — the version of the method that runs is decided at runtime.
When a subclass object is created, the parent class constructor is executed first.
Example:
class A {
A() {
System.out.println("Parent constructor");
}
}
class B extends A {
B() {
System.out.println("Child constructor");
}
}
public class Main {
public static void main(String[] args) {
B obj = new B();
}
}
Output:
Parent constructor
Child constructor
This ensures that the parent class is properly initialized before the child.
private members of a parent class are not inherited.
protected members are inherited but accessible only in subclasses.
public members are fully accessible.
In this tutorial, you learned that inheritance allows one class to acquire the properties and methods of another class. It improves code reuse, organization, and extensibility.
Use extends to create a child class.
Types include single, multilevel, and hierarchical inheritance.
super helps access parent elements.
Method overriding lets child classes customize parent behavior.
Inheritance builds the foundation for polymorphism and other OOP principles in Java.
1. Create a class Person with a method showDetails() that prints the name and age. Create a subclass Employee that adds a salary field and displays all details using inheritance.
2. Write a program where a class Vehicle has a method run(). Create a subclass Bike that overrides the method to print “Bike is running safely.”
3. Create a class Animal with a method eat(). Create two subclasses Dog and Cat that each add their own method (bark() and meow()). Demonstrate hierarchical inheritance.
4. Implement multilevel inheritance:
Class Grandparent with a method greet()
Class Parent inherits Grandparent
Class Child inherits Parent
Call the method greet() from the Child class.
5. Create a program where the parent class Shape has a method area(). Create subclasses Rectangle and Circle that calculate their respective areas.
6. Write a program with a base class Account having a method deposit(). Create a derived class SavingsAccount that adds a method addInterest(). Call both methods from main().
7. Demonstrate the use of the super keyword:
Create a parent class Person with a constructor that prints “Parent class constructor”. Create a child class Student that calls the parent constructor using super().
8. Create a class A with a variable num = 100. Create a subclass B with a variable num = 200. Print both values using super.num and num.
9. Write a program with a class Employee having a method work(). Create a subclass Manager that overrides the work() method. Call both methods to see the difference.
10. Create a parent class Device and a subclass Mobile. The parent class should have a method specs(). Override it in the Mobile class and call both versions using super.specs().