All About Single Inheritance in Java

All About Single Inheritance in Java

3 mins readComment
Esha
Esha Gupta
Associate Senior Executive
Updated on Sep 3, 2024 13:46 IST

Have you ever wondered about the simplicity and efficiency of single inheritance in Java? It allows a class to inherit properties and methods from one parent class, simplifying code development and enhancing reusability. Let's understand more!

Single inheritance in Java is a concept where a class (called the child or subclass) inherits the properties and behaviour (methods and fields) from only one parent class (also known as the superclass). This mechanism allows for code reusability and for the hierarchical organization of classes.

Check out Java Courses Here!

A graphical illustration of single-level inheritance is given below:

Here,

  • Class A: This is the parent class, also known as the superclass.
  • Class B: This is the child class, also known as the subclass.
  • Arrow: The arrow from "Class A" to "Class B" illustrates that "Class B" is inheriting from "Class A"

Types of Inheritance in Java

How Does Single-Level Inheritance Work in Java?

Single-level inheritance in Java allows one class to inherit from another class, creating a parent-child relationship.

Here's how it works:

 

Let's Understand it Via Real-Life Example

Analogy:

Imagine a basic mobile phone passed down to you from a family member. It can make calls and send texts. You decide to add a camera to it, transforming it into a smartphone. Now, this smartphone does everything the basic phone does, plus it can take photos.

Problem Statement:

You need to create a new type of smartphone that includes all the functionalities of a basic mobile phone but with an additional feature: a camera.


 
// This is the superclass representing a basic mobile phone
class BasicPhone {
// Method to make calls
void makeCall() {
System.out.println("Making a call...");
}
// Method to send text messages
void sendText() {
System.out.println("Sending a text...");
}
}
// This is the subclass that extends BasicPhone, adding a new feature: a camera
class Smartphone extends BasicPhone {
// Method to take a photo
void takePhoto() {
System.out.println("Taking a photo...");
}
}
public class InheritanceDemo {
public static void main(String[] args) {
// Create an instance of Smartphone
Smartphone mySmartphone = new Smartphone();
// Use the inherited methods from BasicPhone
mySmartphone.makeCall();
mySmartphone.sendText();
// Use the new method from Smartphone
mySmartphone.takePhoto();
}
}
Copy code

Output

Making a call...
Sending a text...
Taking a photo...

The output demonstrates that the Smartphone object can use all the functionalities of the BasicPhone (making calls and sending texts) because it inherited those methods from the BasicPhone class. Additionally, it shows that the Smartphone has a new feature (taking a photo), which is the new method added to the subclass. This exemplifies single inheritance, where the subclass inherits behaviour from the superclass and also has its own unique behaviour.

Mastering Multilevel Inheritance in Java

Exploring Hierarchical Inheritance in Java

Learning Multiple Inheritance in Java

Understanding Hybrid Inheritance in Java

Difference Between Multilevel and Multiple Inheritance

Why Multiple Inheritance is Not Supported in Java?

Uses and Applications of Single Inheritance in Java

Application Area

Explanation

Basic Calculators

A Calculator class provides basic operations like addition and subtraction. ScientificCalculator extends Calculator to add advanced operations such as trigonometric functions.

Online Retail Accounts

A UserAccount class contains login credentials. CustomerAccount extends UserAccount to add shopping preferences and order history.

Educational Institutions

A Person class has name and age. Student and Teacher classes extend Person to include specifics like grades for students and subjects for teachers.

Software Products

A Product class details a product's basic info. SoftwareProduct extends Product to include version numbers and compatibility details.

Thus, single-level inheritance in Java is a powerful feature that promotes code reuse and organization by allowing a subclass to inherit properties and methods from a single superclass. This mechanism simplifies the development process, enabling developers to build on existing code rather than starting from scratch for every new class.

FAQs

What is single-level inheritance in Java?

Single-level inheritance refers to a scenario where a class (known as a subclass) inherits properties and methods from only one parent class (known as a superclass). This straightforward inheritance model enables the subclass to utilize and extend the functionality defined in the superclass, promoting code reusability and simplicity.

Can a subclass override methods inherited from the superclass in single-level inheritance?

Yes, a subclass can override methods inherited from its superclass. This means the subclass can provide its own implementation of a method that already exists in the superclass, allowing for customized behavior while still maintaining the method's signature. Overriding is a fundamental aspect of polymorphism in object-oriented programming, allowing for dynamic method invocation.

How does single-level inheritance support code reusability in Java?

Single-level inheritance supports code reusability by allowing subclasses to inherit and use properties and methods from their superclass without the need to rewrite the same code. This not only reduces duplication but also facilitates the extension and modification of existing classes, making the development process more efficient and manageable.

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio