DAY11 Multiple Inheritance and Interfaces:
4. Multiple Inheritance Java does not support multiple inheritance with classes to avoid ambiguity caused by the Diamond Problem. However, interfaces allow a class to inherit behavior from multiple sources. This is particularly useful in real-time scenarios where a class needs to combine functionalities from different independent modules.
Interface 1:Logger
Interface 2: Database Operations:
Class to implement above interfaces
Class to demonstrate the Multiple Inheritance:
In the provided Example we have something as interface:
What is Interface and abstract classes and method?
领英推荐
An interface specifies methods that a class must implement but does not provide the actual implementation of those methods. Interfaces are key to achieving abstraction and multiple inheritance in Java.
Now you may have a question how Logger Interface in Multiple Inheritance has implementation in itself:
That is because Interface 1 (Logger) has a default method that provides the implementation for the log method.
Default Methods in Interfaces
Default Methods in Interfaces:
Purpose of Default Methods:
Let's see what are abstract methods and classes tomorrow. Happy Learning :)