Publication Date: February 4, 2025
Reading Time: 4 minutes
Inheritance in Java is a way in which one object gets all the properties and behaviours of a parent object. Single inheritance in Java is a key concept in OOP (Object-Oriented Programming). With inheritance, we can build new classes on top of existing ones. It helps reuse the methods and fields from the parent class while letting you add new methods and fields in the current class. This makes coding cleaner and reduces repetition. Single inheritance in Java is the simplest form of this concept, where one class directly inherits from another.
Inheritance in Java lets a class inherit properties and actions from another class, called the parent class or superclass. The class that gets these features is called the child class or subclass. This means the subclass can use the fields and methods of the parent class, follow reuse rules, and create a clear hierarchy.
Inheritance shows an "IS-A" relationship, also known as a parent-child relationship. It's a fundamental concept that makes coding more straightforward.
Inheritance is essential for several reasons:
In Java, inheritance follows an is-a relationship, meaning one class is a specific type of another class. This relationship is used when a child class logically belongs to a parent class. For example:
In these cases, the child class can inherit properties and behaviors from the parent class. Hence Apple can inherit from Fruit and Dog can inherit from Animal.
A class is like a blueprint or template used to create objects. It defines the common characteristics (like behaviour) and properties (like attributes) all its objects share. A class isn't a real-world entity; it's just the structure for making objects.
The superclass, also called the parent class, is the class whose features are passed down to another class. Think of it as the base class that provides fields and methods to its child classes.
The subclass, or child class, is the one that inherits the features of the parent class. In addition to using the fields and methods of the parent class, it can also add its unique fields and methods, making it more specific.
Inheritance promotes reusability. You don't have to start from scratch if you already have a class with some of the code you need. Instead, you can create a new class that extends the existing one. This way, you reuse the parent class's fields and methods, saving time and effort.
Simply put, inheritance allows code reuse and avoids writing the same logic again. It also makes programs easier to manage by organising classes in a hierarchy. With inheritance, a child class can use the methods and properties of a parent class so duplication can be reduced.
For example, if we have a Vehicle class with a move() method, we don't need to write move() again in Car and Bike classes. They can just inherit it. This makes coding simple, saves time, and helps in software development, where multiple classes share standard features.
There are five types of inheritance:
In this type, a subclass inherits directly from another superclass. It's the simplest form of inheritance where a child class gets all the features of a parent class. It is also called simple inheritance.
This involves a chain of inheritance. A class inherits from a class, and then another class inherits from it. It's like a grandparent, parent, and child relationship.
In this case, multiple child classes inherit from a single parent class. All child classes share the properties and methods of the same parent. It's like multiple classes sharing traits from the same parent.
A class can inherit features from more than one parent class. However, in Java, multiple inheritance is supported only through interfaces to avoid conflicts.
This combines two or more types of inheritance, like hierarchical and multiple. In Java, hybrid inheritance is achieved using interfaces for flexibility and conflict management.
Single inheritance means that a child class inherits from just one parent class. It's the most basic type of inheritance in Java. Therefore, it's also called simple inheritance. In this setup, the child class gets all the properties and behaviours of its parent class.
For example, if there's a parent class named A and a child class named B, the child class B will inherit everything from A. This includes methods and fields, so you don't have to write the same code again. At the same time, the child class can add its own fields and methods. Therefore, it can also make it more specific while still using the foundation provided by the parent class.
The syntax for single inheritance in Java is straightforward. You use the extends keyword to make a child class inherit from a parent class. Here's how it looks:
class Childclass-name extends Parentclass-name {
// methods and fields
}
In this structure, the child class automatically inherits all accessible methods and fields from the parent class. Hence, it is easy to reuse and extend functionality.
Now let's look at a few examples for single inheritance in Java:
// Parent class
class Vehicle {
void start() {
System.out.println("Vehicle is starting...");
}
void stop() {
System.out.println("Vehicle is stopping...");
}
}
// Child class
class Car extends Vehicle {
void honk() {
System.out.println("Car is honking...");
}
}
// Main class
public class Main { // The name matches the file name
public static void main(String[] args) {
Car myCar = new Car(); // Create an object of the Car class
myCar.start(); // Call method from the Vehicle class
myCar.honk(); // Call method from the Car class
myCar.stop(); // Call method from the Vehicle class
}
}
The code shows how a child class can inherit and extend the functionality of a parent class:
Vehicle is starting...
Car is honking...
Vehicle is stopping...
=== Code Execution Successful ===
Let's look at another example of the program for single inheritance in Java. Here an Animal class is the parent class, and a Bird class inherits from it. The Bird class adds the specific behaviour of making a sound, which is a "chirp":
// Parent class
class Animal {
void makeSound() {
System.out.println("Animal makes a sound...");
}
}
// Child class
class Bird extends Animal {
void makeSound() {
System.out.println("Bird chirps...");
}
}
// Main class
public class Main {
public static void main(String[] args) {
Bird myBird = new Bird(); // Create an object of the Bird class
myBird.makeSound(); // Call the overridden method from Bird class
}
}
The code demonstrates how a subclass can inherit and override a method to provide specific functionality.
Bird chirps...
=== Code Execution Successful ===
Here we have an Appliance class (the parent class) and a WashingMachine class (the child class). The washing machine is a type of appliance with a specific behaviour: washing clothes.
// Parent class
class Appliance {
void turnOn() {
System.out.println("Appliance is now ON.");
}
void turnOff() {
System.out.println("Appliance is now OFF.");
}
}
// Child class
class WashingMachine extends Appliance {
void washClothes() {
System.out.println("Washing machine is washing clothes...");
}
}
// Main class
public class Main {
public static void main(String[] args) {
WashingMachine myWasher = new WashingMachine(); // Create an object of the WashingMachine class
myWasher.turnOn(); // Call inherited method from Appliance class
myWasher.washClothes(); // Call method specific to WashingMachine class
myWasher.turnOff(); // Call inherited method from Appliance class
}
}
Appliance is now ON.
Washing machine is washing clothes...
Appliance is now OFF.
=== Code Execution Successful ===
In Java, when a subclass is created, its constructor automatically calls the parent class's constructor. It is done so that the parent class is correctly initialised before the subclass starts adding its own functionality. The super () keyword explicitly calls the parent class's constructor or access its methods and fields.
Now let's look at an example of super () and subclass constructors in single level inheritance in Java:
// Parent class
class Person {
String name;
// Constructor
Person(String name) {
this.name = name;
System.out.println("Person constructor called: Name is " + name);
}
}
// Child class
class Student extends Person {
int grade;
// Constructor
Student(String name, int grade) {
super(name); // Call to parent class constructor
this.grade = grade;
System.out.println("Student constructor called: Grade is " + grade);
}
}
// Main class
public class Main {
public static void main(String[] args) {
Student student = new Student("Aarav", 10); // Create a Student object
}
}
Person constructor called: Name is Aarav
Student constructor called: Grade is 10
=== Code Execution Successful ===
Single inheritance in Java is an important concept that every student and beginner should learn. It lets you understand how to reuse code and keep your programs organised. For new people in coding, it's a simple way to build a strong base in object-oriented programming. By learning single inheritance, you also prepare yourself for bigger topics like multilevel inheritance and polymorphism. To build coding skills, you must also take structured classes with tailored programs. The CCBP 4.0 Academy program is just what you need to build skills and get a leg up in your software career.
Single inheritance in Java means a class (child class) inherits the properties and methods of another class (parent class). It's like passing down the code from one generation to the next.
It makes coding easier by reusing code from the parent class. In a single inheritance in Java program, you don't have to write the same code again and again. It also keeps your program organised.
No, in single inheritance in Java, a class can only inherit from one parent class. If you need multiple parents, Java provides interfaces for that.
The extends keyword is used to show that a class is inheriting from another class. For example, class Bird extends Animal means Bird is inheriting from Animal.
Yes, the child class can add its own methods and properties. Along with inheriting the parent class's features, it can have new ones, too!
Prime Number Program in Java: Explained with Examples - Learn how to write a prime number program in Java with clear logic, optimized algorithms, examples, and interview-ready explanations. (Published: January 4, 2026 | 8 min read)
Why Encapsulation in Java Matters: Learn with Code Snippets - Understand encapsulation in Java, a key object-oriented principle used to restrict direct access and protect internal data from unauthorized changes. (Published: January 4, 2026 | 5 min read)
Master Binary Search in Java: Fast and Efficient Searching Explained - Learn Binary Search in Java with clear logic, easy code examples, and real-world applications. Boost your coding skills with this step-by-step guide! (Published: January 3, 2026 | 5 min read)
Learn Bubble Sort in Java: Easy Sorting Technique Explained - Get a complete guide on bubble sort in Java, including coding examples and tips to understand this basic but important sorting algorithm. (Published: January 2, 2026 | 6 min read)
Best Java Training Institutes in Hyderabad: Your Guide to Career Success - Find the best Java training institutes in Hyderabad for hands-on learning, placement support, and industry-recognized certifications. (Published: January 2, 2026 | 5 min read)
Understanding Inheritance in Java: Key Concepts & Benefits Explained - Learn all about inheritance in Java with clear examples. Understand types, benefits & how it supports code reusability in object-oriented programming. (Published: January 2, 2026 | 8 min read)
Source: NxtWave CCBP Blog
Original URL: https://www.ccbp.in/blog/articles/single-inheritance-in-java
Contact Information: