Learning Multiple Inheritance in Java

Learning Multiple Inheritance in Java

8 mins readComment
Esha
Esha Gupta
Associate Senior Executive
Updated on May 2, 2024 18:12 IST

Have you ever wondered how Java addresses the concept of multiple inheritance and the Diamond Problem? Java avoids these complexities by enabling classes to implement multiple interfaces, allowing for multiple behaviour inheritance without the ambiguity and complications associated with the Diamond Problem seen in traditional multiple class inheritance. Let's understand more!

Multiple inheritance in Java refers to a scenario where a class can inherit properties and methods from more than one superclass. However, Java does not support multiple inheritance directly through classes due to the complexity and ambiguity it can cause. One example of this complexity is the 'Diamond Problem,' where a class inherits from two classes that have a common ancestor, leading to ambiguity regarding which inherited method should be called.

Why Multiple Inheritance is Not Supported in Java?
Why Multiple Inheritance is Not Supported in Java?
Have you ever wondered why Java doesn't support multiple inheritance directly? The primary reason is to avoid complexity and ambiguity in the inheritance hierarchy, especially problems like the diamond issue....read more

Here's a graphical illustration showing multiple inheritance in Java:

Here,

  • Interface A: This is an abstract type that defines a set of method signatures (without implementation) that any implementing class must provide. Interfaces in Java are used to specify the behaviour that a class should implement.
  • Interface B: Similar to Interface A, this is another abstract type with its own set of method signatures. Again, any class that implements this interface is required to provide implementations for the methods declared in the interface.
  • Class C: This is a concrete class that implements both Interface A and Interface B. By doing so, Class C must provide the method implementations for all the methods defined in both interfaces. Class C is an example of how Java simulates multiple inheritance by allowing a class to implement multiple interfaces.
  • Class D: This is another concrete class, and in this context, it extends Class C, which means Class D inherits all the methods and properties from Class C. Through Class C, Class D also indirectly has the contractual obligation to implement the methods from both Interface A and Interface B.

The relationships between the classes and interfaces are as follows:

  • Class C implements Interface A: Class C must provide concrete implementations for all the methods declared in Interface A.
  • Class C implements Interface B: Class C must also provide concrete implementations for all the methods declared in Interface B.
  • Class D extends Class C: Class D inherits from Class C, which means it inherits the implementations of Interface A and Interface B that Class C has provided. If Class C had any concrete methods or properties, Class D would also inherit those.

This diagram depicts multiple inheritance in Java by showing how a class can "inherit" behaviour from more than one interface and how a subclass can extend a class that implements multiple interfaces.

Java does not allow a class to extend more than one other class, but it does allow a class to implement multiple interfaces, which is how Java achieves a form of multiple inheritance.

Types of Inheritance in Java
Types of Inheritance in Java
Inheritance in Java is a fundamental Object-Oriented Programming (OOP) concept that allows one class to inherit fields and methods from another class. Inheritance enables code reuse and establishes a parent-child...read more

How Does Multiple Inheritance Work in Java?

Multiple inheritance, in the traditional sense of a class inheriting from more than one class, does not work in Java because Java does not support this feature directly due to the potential for complexity and ambiguity, such as the "Diamond Problem". Instead, Java provides a way to achieve similar results using interfaces, which allows a class to implement multiple sets of behaviours.

  • Interfaces instead of Classes: Java uses interfaces to define methods that can be implemented by any class. Interfaces only declare methods; they do not provide an implementation.
  • Implementing Multiple Interfaces: A single class in Java can implement multiple interfaces. This means that a class can have multiple sets of method definitions that it agrees to implement.
  • Methods Implementation: For each interface that a class implements, it must provide concrete implementations of all of its abstract methods. If the interfaces contain default methods (with an implementation), the class can either use the default implementation or override it.
  • Interface Inheritance: Interfaces themselves can extend other interfaces, and when they do, they inherit the abstract methods from the parent interface. A single interface can extend multiple other interfaces, thus grouping multiple sets of method declarations.
  • Abstract Classes: Abstract classes are classes that can't be instantiated on their own and can contain a mix of implemented and unimplemented (abstract) methods. A single class can extend one abstract class and implement multiple interfaces, which allows it to use the implemented methods of the abstract class and also to commit to implementing the methods declared by the interfaces.

Let's Understand it Via Real-Life Example

Imagine a Smart Device system where we have different types of smart devices like a SmartSpeaker and a SmartLight. Both devices have common smart functionalities like connecting to a network and performing a system check, but they also have unique features. A SmartSpeaker can play music, while a SmartLight can adjust brightness. We want to design a SmartHomeAssistant that combines the capabilities of both a SmartSpeaker and a SmartLight.


 
// Interface for common smart device functionalities
interface SmartDevice {
void connectToNetwork();
void performSystemCheck();
}
// Interface for functionalities specific to smart speakers
interface MusicPlayer {
void playMusic();
}
// Interface for functionalities specific to smart lights
interface Dimmable {
void setBrightness(int level);
}
// SmartSpeaker class implements both SmartDevice and MusicPlayer
class SmartSpeaker implements SmartDevice, MusicPlayer {
public void connectToNetwork() {
System.out.println("Connecting SmartSpeaker to network.");
}
public void performSystemCheck() {
System.out.println("Performing system check for SmartSpeaker.");
}
public void playMusic() {
System.out.println("Playing music on SmartSpeaker.");
}
}
// SmartLight class implements both SmartDevice and Dimmable
class SmartLight implements SmartDevice, Dimmable {
public void connectToNetwork() {
System.out.println("Connecting SmartLight to network.");
}
public void performSystemCheck() {
System.out.println("Performing system check for SmartLight.");
}
public void setBrightness(int level) {
System.out.println("Setting SmartLight brightness to " + level + ".");
}
}
// SmartHomeAssistant class combines the functionalities of both a SmartSpeaker and a SmartLight
class SmartHomeAssistant implements SmartDevice, MusicPlayer, Dimmable {
// Implementing SmartDevice methods
public void connectToNetwork() {
System.out.println("SmartHomeAssistant connected to network.");
}
public void performSystemCheck() {
System.out.println("SmartHomeAssistant system check complete.");
}
// Implementing MusicPlayer methods
public void playMusic() {
System.out.println("SmartHomeAssistant is playing music.");
}
// Implementing Dimmable methods
public void setBrightness(int level) {
System.out.println("SmartHomeAssistant brightness set to " + level + ".");
}
}
// The main class does not need to be public when compiling in a single-file setup in an online compiler.
class SmartHome {
public static void main(String[] args) {
SmartHomeAssistant assistant = new SmartHomeAssistant();
assistant.connectToNetwork();
assistant.performSystemCheck();
assistant.playMusic();
assistant.setBrightness(5);
}
}
Copy code

Output

SmartHomeAssistant connected to network.
SmartHomeAssistant system check complete.
SmartHomeAssistant is playing music.
SmartHomeAssistant brightness set to 5.

The output shows the SmartHomeAssistant class simulating multiple inheritance by implementing multiple interfaces. When the main method is executed, it creates an instance of SmartHomeAssistant and calls its methods:

  • connectToNetwork() and performSystemCheck() methods show that the assistant is connected to the network and has completed its system check, demonstrating the common SmartDevice functionality.
  • playMusic() method simulates the music-playing feature of a SmartSpeaker.
  • setBrightness(int level) method adjusts the brightness, similar to a SmartLight.

The SmartHomeAssistant is a practical example of how a class in Java can simulate multiple inheritance by implementing multiple interfaces, each providing a different set of capabilities.

Uses and Applications of Multiple Inheritance in Java

Application Area

Explanation

Vehicle Control Systems

In an automotive software system, multiple interfaces like Steerable, Brakeable, and Acceleratable can be implemented by a Car class. This allows the car to inherit steering, braking, and acceleration behaviours from different control systems.

Smart Home Devices

Smart home devices like the SmartHomeAssistant can implement interfaces such as VoiceControllable, WebEnabled, and HomeAutomationCompatible to combine voice control, internet connectivity, and automation features.

Media Players

A media player application can implement interfaces like Playable, Recordable, and Streamable to provide functionalities for playing media, recording, and streaming content. This enables multiple media operations in a single application.

Educational Platforms

An online learning platform can have classes that implement interfaces such as Testable, Graded, and Interactive. This allows a course to have testing capabilities, grading systems, and interactive elements.

Payment Systems

In a payment processing system, a class OnlineTransaction can implement interfaces like Authorizable, Refundable, and Secure. This allows the transaction to incorporate authorization checks, refund processes, and security protocols.

Java does not support multiple inheritance through classes to avoid complexity and ambiguity, it provides a flexible alternative through interfaces. Java's approach encourages designing systems with composition over inheritance, promoting loose coupling and high cohesion. Multiple inheritance through interfaces in Java is widely used in various application areas, enabling developers to create extensible and scalable systems. It allows for the creation of rich interfaces and the ability to mix and match capabilities, which makes Java a powerful object-oriented language suitable for a wide range of programming scenarios.

 

Understanding Hybrid Inheritance in Java
Understanding Hybrid Inheritance in Java
Have you ever wondered about what is hybrid inheritance in Java? It's a technique combining two or more inheritance, allowing a class to inherit properties and behaviours from various sources,...read more

Exploring Hierarchical Inheritance in Java
Exploring Hierarchical Inheritance in Java
Hierarchical inheritance in Java allows exactly this, letting multiple subclasses inherit from a single superclass, enabling both shared functionalities and individual characteristics to coexist harmoniously in a structured and efficient...read more

Mastering Multilevel Inheritance in Java
Mastering Multilevel Inheritance in Java
Do you know multi-level inheritance in Java simplifies building complex systems by letting classes inherit features and behaviours from their ancestors? This method enhances code efficiency and maintainability, enabling developers...read more

Control Statements in Java - About and Types
Control Statements in Java - About and Types
Have you ever wondered how Java manages the flow of its programs so efficiently? The three types of control statements, namely decision-making statements, looping statements, and jump statements, allow Java...read more

Top 160+ Java Interview Questions and Answers for 2024
Top 160+ Java Interview Questions and Answers for 2024
This article consists of the Top 160 Java interview questions and answers, covering all the important topics such as Java features, Core Java concepts, OOPs concepts, collections, access specifiers, threads, exceptions,...read more

Check out Java courses here!

FAQs

What is multiple inheritance in Java?

Multiple inheritance in Java is the ability for a class to inherit behaviors from multiple types. However, Java does not support multiple inheritance of state (fields/properties) through classes. Instead, it allows a class to implement multiple interfaces, which can define methods without state.

Why doesn't Java support multiple inheritance through classes?

Java avoids multiple inheritance through classes to prevent complexity and ambiguity, especially the "Diamond Problem," where a class inherits from two classes that have a common ancestor. This can lead to conflicts in method resolution and state management. Interfaces in Java provide a clean workaround by allowing a class to inherit multiple behaviors without the associated risks.

Can a Java class implement multiple interfaces?

Yes, a Java class can implement multiple interfaces. This is Java's way of allowing a class to have multiple types and behaviors. When a class implements multiple interfaces, it must provide concrete implementations for all the abstract methods declared in the interfaces unless the methods have default implementations.

How do default methods in interfaces affect multiple inheritance in Java?

Default methods, introduced in Java 8, allow interfaces to provide a default implementation for methods. This adds more flexibility and enables a form of multiple inheritance of behavior. If a class implements multiple interfaces with conflicting default methods, the class must override the conflicting method to resolve the ambiguity.

Can interfaces in Java extend other interfaces?

Yes, interfaces in Java can extend one or more other interfaces. When an interface extends another, it inherits all the abstract methods from its parent interfaces. This allows for a hierarchical structure of interfaces, enabling complex behaviors to be built and refined across different levels of abstraction.

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