Last Updated : 30 Jul, 2025
Try it on GfG Practice
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from another class can reuse the methods and fields of that class.
Example: In the following example, Animal is the base class and Dog, Cat and Cow are derived classes that extend the Animal class.
Implementation:
Java
// Parent class
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
// Child class
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
// Child class
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
// Child class
class Cow extends Animal {
void sound() {
System.out.println("Cow moos");
}
}
// Main class
public class Geeks {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.sound();
a = new Cat();
a.sound();
a = new Cow();
a.sound();
}
}
Dog barks Cat meows Cow moos
Explanation:
Syntax JavaNote: In practice, inheritance and polymorphism are used together in Java to achieve fast performance and readability of code.
class ChildClass extends ParentClass {
// Additional fields and methods
}
Why Use Inheritance in Java?Note: In Java, inheritance is implemented using the extends keyword. The class that inherits is called the subclass (child class) and the class being inherited from is called the superclass (parent class).
The extends keyword is used for inheritance in Java. It enables the subclass to inherit the fields and methods of the superclass. When a class extends another class, it means it inherits all the non-primitive members (fields and methods) of the parent class and the subclass can also override or add new functionality to them.
Types of Inheritance in Java Types of Inheritance in JavaNote: The extends keyword establishes an "is-a" relationship between the child class and the parent class. This allows a child class to have all the behavior of the parent class.
Below are the different types of inheritance which are supported by Java.
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.
Example:
Java
//Super class
class Vehicle {
Vehicle() {
System.out.println("This is a Vehicle");
}
}
// Subclass
class Car extends Vehicle {
Car() {
System.out.println("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();
}
}
This is a Vehicle This Vehicle is Car2. 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.
Example:
Java
class Vehicle {
Vehicle() {
System.out.println("This is a Vehicle");
}
}
class FourWheeler extends Vehicle {
FourWheeler() {
System.out.println("4 Wheeler Vehicles");
}
}
class Car extends FourWheeler {
Car() {
System.out.println("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
}
}
This is a Vehicle 4 Wheeler Vehicles This 4 Wheeler Vehicle is a Car3. 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
Example:
Java
class Vehicle {
Vehicle() {
System.out.println("This is a Vehicle");
}
}
class Car extends Vehicle {
Car() {
System.out.println("This Vehicle is Car");
}
}
class Bus extends Vehicle {
Bus() {
System.out.println("This Vehicle is Bus");
}
}
public class Test {
public static void main(String[] args) {
Car obj1 = new Car();
Bus obj2 = new Bus();
}
}
This is a Vehicle This Vehicle is Car This is a Vehicle This Vehicle is Bus4. 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.
Example:
Java
interface LandVehicle {
default void landInfo() {
System.out.println("This is a LandVehicle");
}
}
interface WaterVehicle {
default void waterInfo() {
System.out.println("This is a WaterVehicle");
}
}
// Subclass implementing both interfaces
class AmphibiousVehicle implements LandVehicle, WaterVehicle {
AmphibiousVehicle() {
System.out.println("This is an AmphibiousVehicle");
}
}
public class Test {
public static void main(String[] args) {
AmphibiousVehicle obj = new AmphibiousVehicle();
obj.waterInfo();
obj.landInfo();
}
}
This is an AmphibiousVehicle This is a WaterVehicle This is a LandVehicle5. 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.
IS-A represents an inheritance relationship in Java, meaning this object is a type of that object.
Java
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:
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();
System.out.println(s instanceof SolarSystem);
System.out.println(e instanceof Earth);
System.out.println(m instanceof SolarSystem);
}
}
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:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4