0% found this document useful (0 votes)
6 views10 pages

Inheritance in Java - GeeksforGeeks

Inheritance in Java is a fundamental object-oriented programming concept that allows a class to inherit properties and behaviors from another class, promoting code reusability and organization. The document explains various types of inheritance, including single, multilevel, hierarchical, multiple (through interfaces), and hybrid inheritance, along with examples for each type. It also discusses the advantages and disadvantages of inheritance, such as code reusability and complexity.

Uploaded by

Unknown
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Inheritance in Java - GeeksforGeeks

Inheritance in Java is a fundamental object-oriented programming concept that allows a class to inherit properties and behaviors from another class, promoting code reusability and organization. The document explains various types of inheritance, including single, multilevel, hierarchical, multiple (through interfaces), and hybrid inheritance, along with examples for each type. It also discusses the advantages and disadvantages of inheritance, such as code reusability and complexity.

Uploaded by

Unknown
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2/11/26, 8:34 PM Inheritance in Java - GeeksforGeeks

Tutorials
Search... Practice
Sign In
Jobs
Java Tutorial Advanced Java Interview Questions Exercises Examples Quizzes Projects Cheatsheet DSA in Java Java Collection

Inheritance in Java
Last Updated : 22 Jan, 2026

Inheritance in Java is a core OOP concept that allows a class to acquire properties and behaviors
from another class. It helps in creating a new class from an existing class, promoting code reusability
and better organization.

A subclass can reuse the fields and methods of the parent class without rewriting the code
A subclass can add its own fields and methods or modify existing ones to extend functionality.

Example: In the following example, Animal is the base class and Dog, Cat, and Cow are derived
classes that extend the Animal class.

// Parent class
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}

// Child class
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}

// Child class
class Cat extends Animal {
void sound() {
[Link]("Cat meows");
}
}

// Child class
class Cow extends Animal {
void sound() {
[Link]("Cow moos");
}
}

// Main class
public class Geeks {
public static void main(String[] args) {
Animal a;
a = new Dog();
[Link]();

a = new Cat();
[Link]();

a = new Cow();
[Link]();

[Link] 1/10
2/11/26, 8:34 PM Inheritance in Java - GeeksforGeeks
}
}

Output

Dog barks
Cat meows
Cow moos

Explanation:

Animal is the base class.


Dog, Cat and Cow are derived classes that extend Animal class and provide specific
implementations of the sound() method.
The Geeks class is the driver class that creates objects and demonstrates runtime polymorphism
using method overriding.

Inheritance in Java

Syntax

class Parent {
// fields and methods
}
class Child extends Parent {
// additional fields and methods
}

Note: In Java, inheritance is implemented using the extends keyword.

Key Terminologies in Java Inheritance

Term Description

Class Blueprint from which objects are created

[Link] 2/10
2/11/26, 8:34 PM Inheritance in Java - GeeksforGeeks

Term Description

Superclass Class whose properties are inherited

Subclass Class that inherits another class

extends Keyword used to inherit a class

Types of Inheritance in Java

Types of Inheritance in Java

Below are the different types of inheritance which are supported by Java.

1. Single Inheritance

In single inheritance, a sub-class is derived from only one super class. It inherits the properties and
behavior of a single-parent class. Sometimes, it is also known as simple inheritance.

[Link] 3/10
2/11/26, 8:34 PM Inheritance in Java - GeeksforGeeks

Single Inheritance

//Super class
class Vehicle {
Vehicle() {
[Link]("This is a Vehicle");
}
}

// Subclass
class Car extends Vehicle {
Car() {
[Link]("This Vehicle is Car");
}
}

public class Test {


public static void main(String[] args) {
// Creating object of subclass invokes base class constructor
Car obj = new Car();
}
}

Output

This is a Vehicle
This Vehicle is Car

2. Multilevel Inheritance

In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived
class also acts as the base class for other classes.

[Link] 4/10
2/11/26, 8:34 PM Inheritance in Java - GeeksforGeeks

Multilevel Inheritance

class Vehicle {
Vehicle() {
[Link]("This is a Vehicle");
}
}
class FourWheeler extends Vehicle {
FourWheeler() {
[Link]("4 Wheeler Vehicles");
}
}
class Car extends FourWheeler {
Car() {
[Link]("This 4 Wheeler Vehicle is a Car");
}
}
public class Geeks {
public static void main(String[] args) {
Car obj = new Car(); // Triggers all constructors in order
}
}

Output

This is a Vehicle
4 Wheeler Vehicles
This 4 Wheeler Vehicle is a Car

3. Hierarchical Inheritance

In hierarchical inheritance, more than one subclass is inherited from a single base class. i.e. more
than one derived class is created from a single base class. For example, cars and buses both are
vehicle

[Link] 5/10
2/11/26, 8:34 PM Inheritance in Java - GeeksforGeeks

Hierarchical Inheritance

class Vehicle {
Vehicle() {
[Link]("This is a Vehicle");
}
}

class Car extends Vehicle {


Car() {
[Link]("This Vehicle is Car");
}
}

class Bus extends Vehicle {


Bus() {
[Link]("This Vehicle is Bus");
}
}

public class Test {


public static void main(String[] args) {
Car obj1 = new Car();
Bus obj2 = new Bus();
}
}

Output

This is a Vehicle
This Vehicle is Car
This is a Vehicle
This Vehicle is Bus

4. Multiple Inheritance (Through Interfaces)

In Multiple inheritances, one class can have more than one superclass and inherit features from all
parent classes.

Note: that Java does not support multiple inheritances with classes. In Java, we can achieve
multiple inheritances only through Interfaces.

[Link] 6/10
2/11/26, 8:34 PM Inheritance in Java - GeeksforGeeks

Multiple Inheritance

interface LandVehicle {
default void landInfo() {
[Link]("This is a LandVehicle");
}
}
interface WaterVehicle {
default void waterInfo() {
[Link]("This is a WaterVehicle");
}
}
// Subclass implementing both interfaces
class AmphibiousVehicle implements LandVehicle, WaterVehicle {
AmphibiousVehicle() {
[Link]("This is an AmphibiousVehicle");
}
}
public class Test {
public static void main(String[] args) {
AmphibiousVehicle obj = new AmphibiousVehicle();
[Link]();
[Link]();
}
}

Output

This is an AmphibiousVehicle
This is a WaterVehicle
This is a LandVehicle

5. Hybrid Inheritance

It is a mix of two or more of the above types of inheritance. In Java, we can achieve hybrid inheritance
only through Interfaces if we want to involve multiple inheritance to implement Hybrid inheritance.

[Link] 7/10
2/11/26, 8:34 PM Inheritance in Java - GeeksforGeeks

Hybrid Inheritance

Explanation:
class Car extends Vehicle->Single Inheritance
class Bus extends Vehicle and class Bus implements Interface Fare->Hybrid Inheritance (since
Bus inherits from two sources, forming a combination of single + multiple inheritance).

Java IS-A type of Relationship

IS-A represents an inheritance relationship in Java, meaning this object is a type of that object.

public class SolarSystem {


}
public class Earth extends SolarSystem {
}
public class Mars extends SolarSystem {
}
public class Moon extends Earth {
}

Now, based on the above example, in Object-Oriented terms, the following are true:

SolarSystem is the superclass of Earth class.


SolarSystem is the superclass of Mars class.
Earth and Mars are subclasses of SolarSystem class.
Moon is the subclass of both Earth and SolarSystem classes.

class SolarSystem {
}
class Earth extends SolarSystem {
}
class Mars extends SolarSystem {
}
public class Moon extends Earth {
public static void main(String args[])
{
SolarSystem s = new SolarSystem();
Earth e = new Earth();
Mars m = new Mars();

[Link](s instanceof SolarSystem);


[Link](e instanceof Earth);
[Link](m instanceof SolarSystem);
}
[Link] 8/10
2/11/26, 8:34 PM Inheritance in Java - GeeksforGeeks
}

Output

true
true
true

What Can Be Done in a Subclass?

In sub-classes we can inherit members as is, replace them, hide them or supplement them with new
members:

The inherited fields can be used directly, just like any other fields.
We can declare new fields in the subclass that are not in the superclass.
The inherited methods can be used directly as they are.
We can write a new instance method in the subclass that has the same signature as the one in
the superclass, thus overriding it (as in the example above, toString() method is overridden).
We can write a new static method in the subclass that has the same signature as the one in the
superclass, thus hiding it.
We can declare new methods in the subclass that are not in the superclass.
We can write a subclass constructor that invokes the constructor of the superclass, either
implicitly or by using the keyword super.

Advantages of Inheritance in Java

Code Reusability: Inheritance allows for code reuse and reduces the amount of code that needs to
be written. The subclass can reuse the properties and methods of the superclass, reducing
duplication of code.
Abstraction: Inheritance allows for the creation of abstract classes that define a common
interface for a group of related classes. This promotes abstraction and encapsulation, making the
code easier to maintain and extend.
Class Hierarchy: Inheritance allows for the creation of a class hierarchy, which can be used to
model real-world objects and their relationships.
Polymorphism: Inheritance allows for polymorphism, which is the ability of an object to take on
multiple forms. Subclasses can override the methods of the superclass, which allows them to
change their behavior in different ways.

Disadvantages of Inheritance in Java

Complexity: Inheritance can make the code more complex and harder to understand. This is
especially true if the inheritance hierarchy is deep or if multiple inheritances is used.
Tight Coupling: Inheritance creates a tight coupling between the superclass and subclass, making
it difficult to make changes to the superclass without affecting the subclass.

[Link] 9/10
2/11/26, 8:34 PM Inheritance in Java - GeeksforGeeks

Company Explore Tutorials Courses Offline Preparation


About Us POTD Programming ML and Data Centers Corner
Corporate & Communications Address: Legal Practice Languages Science Noida Interview
Privacy Problems DSA DSA and Bengaluru Corner
A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Policy Connect Web Placements Pune Aptitude
Pradesh (201305) Careers Blogs Technology Web Hyderabad Puzzles
Contact Us 90% AI, ML & Development Kolkata GfG 160
Registered Address:
Corporate Refund Data Science Data Science System Design
K 061, Tower K, Gulshan Vivante Solution on DevOps Programming
Apartment, Sector 137, Noida, Gautam
Campus Courses CS Core Languages
Buddh Nagar, Uttar Pradesh, 201305
Training Subjects DevOps &
Program GATE Cloud
School GATE
Subjects Trending
Software and Technologies
Tools
Inheritance with Java Visit Course

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

Suggested Quiz 4 Questions

Which keyword is used for inheritance in Java?

A inherit

B implements

C super

D extends

1/4 < Previous Next >

Comment K kartik Follow 695

Article Tags: Java java-inheritance

[Link] 10/10

You might also like