⏱️ 20 min read • Beginner Level • Lesson 50
Completed on . You can revise this lesson or continue to the next topic.
Inheritance in Java is an important object-oriented programming concept where one class can acquire the properties and behaviors of another class. It helps you reuse code, organize classes, and build relationships between objects.
In this lesson, you will learn Java inheritance deeply with the extends keyword,
parent and child classes, types of inheritance, constructor execution order, super keyword,
method overriding, protected access, upcasting, tricky cases, common mistakes, and interview questions.
Before learning inheritance, you should understand
Java Class and Objects,
Java Class Members,
Java Constructors, and
Java this Keyword.
Inheritance is a mechanism in Java where one class can use the variables and methods of another class. The class that gives members is called the parent class, and the class that receives members is called the child class.
extends keyword.
Think about a general Vehicle. A Car is a type of Vehicle, and a Bike
is also a type of Vehicle.
Vehicle
/ \
Car Bike
\
SportsBike
Common features like speed, fuel, start, and stop.
Inherits vehicle features and adds car-specific features.
Inherits vehicle features and adds bike-specific features.
Inheritance is used when multiple classes share common properties or behavior. Instead of writing the same code again and again, common code is written in a parent class.
| Benefit | Meaning |
|---|---|
| Code Reusability | Common code can be written once in the parent class and reused by child classes. |
| Cleaner Design | Classes can be arranged in a logical parent-child relationship. |
| Extensibility | Existing code can be extended without modifying the parent class. |
| Polymorphism Support | Inheritance allows method overriding and runtime polymorphism. |
| Term | Meaning |
|---|---|
| Parent Class | The class whose members are inherited. Also called superclass or base class. |
| Child Class | The class that inherits members. Also called subclass or derived class. |
extends |
Keyword used to inherit a class. |
super |
Keyword used to refer to parent class members or constructor. |
| IS-A Relationship | A relationship where child is a specific type of parent. |
class ParentClass {
// parent class members
}
class ChildClass extends ParentClass {
// child class members
}
extends keyword to inherit one class from another class.
class Animal {
void eat() {
System.out.println("Animal eats food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}
Output:
Animal eats food Dog barks
Animal is the parent class.Dog is the child class.Dog extends Animal means Dog inherits Animal.Dog object can call both eat() and bark().Java supports inheritance in different forms. Some are directly supported through classes, and some are achieved through interfaces.
| Type | Supported with Classes? | Meaning |
|---|---|---|
| Single Inheritance | Yes | One child class inherits one parent class. |
| Multilevel Inheritance | Yes | A class inherits from a child class, forming a chain. |
| Hierarchical Inheritance | Yes | Multiple child classes inherit the same parent class. |
| Multiple Inheritance | No, with classes | A class cannot extend multiple classes in Java. |
| Hybrid Inheritance | Through interfaces | A combination of inheritance types, usually achieved using interfaces. |
Single inheritance means one child class inherits from one parent class.
class Vehicle {
void start() {
System.out.println("Vehicle starts");
}
}
class Car extends Vehicle {
void drive() {
System.out.println("Car drives");
}
public static void main(String[] args) {
Car car = new Car();
car.start();
car.drive();
}
}
Output:
Vehicle starts Car drives
Vehicle is the parent class.
Car inherits Vehicle using the extends keyword.
Car object can access both:
start()drive()start() belongs to the Vehicle class.
drive() belongs to the Car class.
Multilevel inheritance creates a chain of inheritance. For example, Dog inherits from
Mammal, and Mammal inherits from Animal.
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
class Mammal extends Animal {
void walk() {
System.out.println("Mammal walks");
}
}
class Dog extends Mammal {
void bark() {
System.out.println("Dog barks");
}
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.walk();
dog.bark();
}
}
Output:
Animal eats Mammal walks Dog barks
In multilevel inheritance, the child class object contains accessible members from all classes in the inheritance chain.
Animal members
Mammal members
Dog members
Animal is the top-level parent class.Mammal extends Animal.Dog extends Mammal.Dog object can access eat(), walk(), and bark().Hierarchical inheritance means multiple child classes inherit from the same parent class.
class Shape {
void draw() {
System.out.println("Drawing shape");
}
}
class Circle extends Shape {
void circleInfo() {
System.out.println("This is a circle");
}
}
class Rectangle extends Shape {
void rectangleInfo() {
System.out.println("This is a rectangle");
}
public static void main(String[] args) {
Circle c = new Circle();
c.draw();
c.circleInfo();
Rectangle r = new Rectangle();
r.draw();
r.rectangleInfo();
}
}
Output:
Drawing shape This is a circle Drawing shape This is a rectangle
In hierarchical inheritance, multiple child classes inherit from the same parent class. This means common functionality is written once in the parent class and reused by many child classes.
Shape
Circle
Rectangle
Shape is the parent class.Circle inherits from Shape.Rectangle also inherits from Shape.draw() method belongs to the Shape class.Circle and Rectangle objects can access draw().circleInfo() belongs only to the Circle class.rectangleInfo() belongs only to the Rectangle class.Java does not support multiple inheritance using classes. This means one class cannot extend two classes at the same time.
| Language | Multiple Inheritance using Classes |
|---|---|
| Java | ❌ No |
| C++ | ✅ Yes |
| Python | ✅ Yes |
class A {
}
class B {
}
class C extends A, B { // Error
}
Output:
Compilation Error
When a child class object is created, the parent class constructor executes first, then the child class constructor executes.
class Parent {
Parent() {
System.out.println("Parent constructor");
}
}
class Child extends Parent {
Child() {
System.out.println("Child constructor");
}
public static void main(String[] args) {
Child obj = new Child();
}
}
Output:
Parent constructor Child constructor
new Child()
The super keyword refers to the parent class part of the current object.
It is commonly used to call parent class constructor, parent class method, or parent class variable.
✓ call parent constructor
✓ call parent method
✓ access parent variable
class Parent {
void show() {
System.out.println("Parent show method");
}
}
class Child extends Parent {
void show() {
super.show();
System.out.println("Child show method");
}
public static void main(String[] args) {
Child c = new Child();
c.show();
}
}
Output:
Parent show method Child show method
super keyword will be explained deeply in the next Java OOP lesson.
Method overriding happens when a child class provides its own implementation of a method that already exists in the parent class.
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
Output:
Dog barks
In method overriding, the child class provides its own version of a method that already exists in the parent class.
Animal defines sound()
Dog overrides sound()
Dog implementation executes
Animal class defines the sound() method.Dog class extends Animal.Dog class provides its own implementation of sound().d.sound() is called, Java executes the Dog class version.
A protected member can be accessed in the same package and also in child classes.
It is often used when parent class data or behavior should be available to subclasses.
class Employee {
protected double salary = 50000;
}
class Developer extends Employee {
void showSalary() {
System.out.println("Salary: " + salary);
}
public static void main(String[] args) {
Developer d = new Developer();
d.showSalary();
}
}
Output:
Salary: 50000.0
Upcasting means storing a child class object in a parent class reference variable. This is allowed because the child object is also a type of parent.
A Dog object is also an Animal because Dog extends
Animal. Therefore, storing a Dog object in an
Animal reference is called upcasting.
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}
Output:
Dog barks
Downcasting means converting a parent class reference back into a child class reference. It requires explicit casting.
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
public static void main(String[] args) {
Animal a = new Dog(); // Upcasting
Dog d = (Dog) a; // Downcasting
d.bark();
}
}
Output:
Dog barks
Use inheritance only when the child class is truly a type of the parent class.
| Relationship | Correct? | Reason |
|---|---|---|
| Dog is an Animal | Yes | Dog is a specific type of Animal. |
| Car is a Vehicle | Yes | Car is a specific type of Vehicle. |
| Car is an Engine | No | Car has an Engine. This is HAS-A, not IS-A. |
class Parent {
private int number = 10;
}
class Child extends Parent {
void show() {
System.out.println(number); // Error
}
}
Output:
Compilation Error
class Parent {
Parent(String name) {
System.out.println(name);
}
}
class Child extends Parent {
Child() {
System.out.println("Child constructor");
}
}
Output:
Compilation Error
super() automatically, but the parent class does not have a no-argument constructor.
The child constructor must call super("value") explicitly.
final class Parent {
}
class Child extends Parent { // Error
}
Output:
Compilation Error
final class cannot be inherited.
super() when parent has only parameterized constructor.extends keyword is used for class inheritance.super is used to access parent class members and constructors.🧠 Test your understanding with a quick quiz
Topic: Inheritance | Language: Java
class Parent {
Parent() {
System.out.println("Parent constructor");
}
}
class Child extends Parent {
Child() {
System.out.println("Child constructor");
}
public static void main(String[] args) {
new Child();
}
}class Animal {
void eat() {
System.out.println("Animal eats food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}class Parent {
void show() {
System.out.println("Parent show");
}
}
class Child extends Parent {
void show() {
super.show();
System.out.println("Child show");
}
public static void main(String[] args) {
new Child().show();
}
}class A { }
class B { }
class C extends A, B { }🎉 Great job! Continue learning Java OOP step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.