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


Defines a default method for logging

Interface 2: Database Operations:


provide abstract method for database connections.


Class to implement above interfaces

Implements both

  • Combines logging and database functionalities.
  • Implements test logic with shared utilities.

Class to demonstrate the Multiple Inheritance:

Demonstrates how the class combines functionalities from multiple interfaces.


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.


Interface Example


  • Interface methods do not have a body - the body is provided by the "implement" class
  • On implementation of an interface, you must override all of its methods
  • Interface methods are by default abstract and public
  • Interface attributes are by default public, static and final
  • An interface cannot contain a constructor (as it cannot be used to create objects)



Implementation class of above Interface example



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:

  • Before Java 8, interfaces could only contain abstract methods, meaning any class implementing an interface had to provide the implementation.
  • From Java 8 onwards, interfaces can have default methods with a body (implementation). This allows the interface to define a common behavior while still being extensible by the implementing classes.

Purpose of Default Methods:

  • They enable backward compatibility in Java. New methods can be added to existing interfaces without breaking the classes that already implement the interface.
  • Allow interfaces to provide reusable functionality, reducing the need for implementing classes to redefine common behavior.


Let's see what are abstract methods and classes tomorrow. Happy Learning :)

要查看或添加评论,请登录

Steffi Graph Preethi S的更多文章

  • Arrays

    Arrays

    Been so long since I posted, Happy that i'm able to be back with basic concepts again. Let's see what is Arrays: As we…

  • Day 19: StringBuffer and StringBuilder in Java:

    Day 19: StringBuffer and StringBuilder in Java:

    Mutable String: A String that can be modified or changed is known as mutable String. and are classes in Java used for…

  • Day 18: Strings:

    Day 18: Strings:

    String is a class in the java.lang package and is one of the most commonly used classes.

  • Day 17:

    Day 17:

    Static Keyword: Static is a keyword in Java. The keyword is used to indicate that a particular member (method…

  • Day 16: Jump Statements

    Day 16: Jump Statements

    Jump Statements These statements alter the normal flow of control by transferring execution. 1.

  • Day 16:

    Day 16:

    Looping Statements Looping statement are the execution of the block of code repeatedly until we break it. for Loop…

  • Day 15:

    Day 15:

    Java Control Statements: Java compiler executes the code from Top to Bottom. However Java provides the statement to…

  • Day 14: Encapsulation

    Day 14: Encapsulation

    Day 14: Encapsulation: Encapsulation in Java is integrating the data (fields) and the methods that operate on the data…

  • DAY 13 Polymorphism:

    DAY 13 Polymorphism:

    What is Polymorphism? In simple terms, polymorphism means "many forms," enabling a single method to work in different…

  • Abstract methods and abstract classes

    Abstract methods and abstract classes

    Day 12: What is Abstraction? Hiding the Implementation details. Interface allows 100% Abstraction, whereas Abstraction…

社区洞察

其他会员也浏览了