Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Terms frequently used in Inheritance
2.
What is Single Inheritance?
2.1.
Syntax of Single Inheritance in Java
2.2.
Code to illustrate Single Inheritance in Java
2.3.
Java
2.4.
Explanation
3.
Benefits of of Single Inheritance in Java
4.
Limitations of of Single Inheritance in Java
5.
Examples of Single Inheritance in Java Programming
5.1.
Java
5.2.
Java
6.
Using Super() and Subclass Constructors in Single Inheritance in Java
6.1.
Java
7.
Frequently Asked Questions
7.1.
What is a real life example of single inheritance in Java?
7.2.
What is inheritance single & multilevel in Java?
7.3.
What are the 4 types of inheritance in Java?
8.
Conclusion
Last Updated: Jul 11, 2024
Easy

Single Inheritance in Java

Author Yukti Kumari
6 upvotes

Introduction

Single Inheritance in Java simplifies class hierarchies by allowing a subclass to inherit properties and behaviors from a single superclass. It's achieved using the 'extends' keyword, promoting clean and maintainable code.

single inheritance program in java

Inheritance allows one class to inherit the methods and variables from other classes, thus reusing the codes. In Java, we have different types of inheritance, namely, single inheritance, multiple, multilevel, and hybrid. Inheritance establishes an “is-a” relationship between two classes or a “parent-child” relationship.

Example - 

Suppose we have a class named “Human” and another class, “Employee”. Since an Employee IS A Human, the employee class can inherit the methods and fields of the human class.

In this article, we will go through the basic understanding of single inheritance in java along with examples and implementation.

Terms frequently used in Inheritance

  • Class: It is a user-defined template or blueprint from which objects are created.
     
  • Derived/Sub/Child class: It is a class that is derived from another class. Also known as extended class.
     
  • Base/Super/Parent class: It is a class from which the derived class inherits its features.
     
  • Reusability: It allows the reuse of the methods and fields of the existing class when creating a new class.

What is Single Inheritance?

Single inheritance is the simplest type of inheritance in java. In this, a class inherits the properties from a single class. The class which inherits is called the derived class or child class or subclass, while the class from which the derived class inherits is called the base class or superclass or parent class. So, in single inheritance, we have only one derived class and one base class.

Let us look at this diagram to understand the relation : 

 

Single Inheritance

Syntax of Single Inheritance in Java

class base class
{
     .... methods
}
class derivedClass name extends baseClass
{
    methods ... along with this additional feature
}
You can also try this code with Online Java Compiler
Run Code

Java uses the keyword “extends” to create a new class(derived class) from the existing class(base class). The term “extends” means to increase the functionality as the derived class can reuse the methods and fields of the base class, and along with this, new methods and fields can also be defined in the derived class, hence increasing the functionality.

Code to illustrate Single Inheritance in Java

  • Java

Java

class Employee {
void salary() {
System.out.println("Salary= 200000");
}
}

class Programmer extends Employee {
// Programmer class inherits from Employee class
void bonus() {
System.out.println("Bonus=50000");
}
}

class single_inheritance {
public static void main(String args[]) {
Programmer p = new Programmer();
p.salary(); // calls method of super class
p.bonus(); // calls method of sub class
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Salary= 200000
Bonus=50000


Try this code by yourself on Online Java Compiler.

Explanation

In the above example, we have two classes- The employee class and the Programmer class. 

We see that the Programmer class extends the Employee class, which is an example of single inheritance. The relationship “is a” is established here, i.e., a Programmer is an Employee. In the main method, we create an object ‘p’ of the class Programmer. p.salary() calls the method of the Employee class, which is inherited by the programmer class, and p.bonus() calls the method of the programmer class. 

Single inheritance in Java has several benefits and limitations. Here are some of them:

Also see, Characteristics of OOPS

Benefits of of Single Inheritance in Java

  • It makes code easier to understand and manage by maintaining a hierarchical structure. Code duplicity is also reduced.
  • Code reusability, as a derived class, can inherit the properties, behaviours and methods from the parent class.
  • A higher level of abstraction, as the derived class can add its own methods and properties with the addition of the properties inherited from the parent class.

Limitations of of Single Inheritance in Java

  • a class can only inherit from a single parent which results in less flexibility for class designs.
  • limit the ability to reuse code as some code might not fit in a hierarchy caused due to single inheritance.
  • This can cause ambiguity in code or throw errors, as some inherited methods can have the same names but have different operations.

Examples of Single Inheritance in Java Programming

Let us take two examples of single inheritance:

Example 1:

  • Java

Java

class Animal {
void eat() {
System.out.println("Animal is eating");
}
}

class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}

class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited from Animal
myDog.bark();
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

output

Example 2:

  • Java

Java

class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}

class Circle extends Shape {
void drawCircle() {
System.out.println("Drawing a circle");
}
}

public class Main {
public static void main(String[] args) {
Circle myCircle = new Circle();
myCircle.draw(); // Inherited from Shape
myCircle.drawCircle();
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

output

Using Super() and Subclass Constructors in Single Inheritance in Java

In Java, when implementing single inheritance, the super() keyword is used in a subclass constructor to invoke the constructor of its superclass. This is crucial for initializing inherited attributes and ensuring proper construction of the object hierarchy.

Consider the following example:

  • Java

Java

class Animal {
String sound;

Animal(String sound) {
this.sound = sound;
}

void makeSound() {
System.out.println("Animal makes a sound: " + sound);
}
}

class Dog extends Animal {
String breed;

Dog(String sound, String breed) {
super(sound); // Invoking superclass constructor
this.breed = breed;
}

void displayInfo() {
System.out.println("Dog breed: " + breed);
}
}

class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Woof", "Labrador");
myDog.makeSound(); // Inherited from Animal
myDog.displayInfo();
}
}
You can also try this code with Online Java Compiler
Run Code

Output

output

In this example, the Dog class extends the Animal class. The super(sound) line in the Dog constructor invokes the constructor of the Animal class, initializing the sound attribute. This demonstrates the use of super() and subclass constructors in the context of single inheritance in Java.

Frequently Asked Questions

What is a real life example of single inheritance in Java?

An example is a "Car" class inheriting from a "Vehicle" class. The "Car" class gains properties like speed and methods like start() from the "Vehicle" class.

What is inheritance single & multilevel in Java?

Single inheritance involves a class inheriting from one superclass, while multilevel inheritance includes a chain of classes, each extending the one above it. In Java, this forms a hierarchy facilitating code organization and reuse.

What are the 4 types of inheritance in Java?

The four types of inheritance in Java are single, multilevel, hierarchical, and hybrid. Single, multilevel, and hierarchical are straightforward, while hybrid involves combining these types, typically using interfaces.

Conclusion

In this article, we discussed Single Inheritance in Java, an important concept in Object-oriented programming, how it works, the syntax, and examples of implementing single inheritance in Java. 

You can learn more about Object-Oriented Programming and related concepts here.


You can also learn programming in java from scratch from our guided path -” Basics of Java,” which explains everything needed to get started with programming in java, like functions, loops,  arrays, strings, and other Data Structures and Algorithms. 

Don’t forget to test your problem-solving skills by practicing problems from Coding Ninjas Studio, and also check out some Java Interview Questions as well as the numerous interview experiences specially curated to give you the right guidance.

Happy Coding!

Live masterclass