S07L03 – Interface with Polymorphism and multiple inheritance

Interface with Polymorphism and Multiple Inheritance in Java

Table of Contents

1. Introduction

In Java, interfaces provide a way to achieve abstraction, and along with polymorphism, they enable multiple inheritance. Polymorphism allows objects to be treated as instances of their parent class or interface, which enhances flexibility in application design.

This article will focus on a practical example to demonstrate how interfaces can help manage multiple inheritance and polymorphism through a well-structured Java program. The example project defines different types of phones, showing how they share behavior through interfaces, yet allow for specific implementations in subclasses.

2. Key Concepts of Interface, Polymorphism, and Multiple Inheritance

Term Definition
Interface A contract that defines methods a class must implement without specifying how.
Polymorphism The ability of an object to take on many forms, typically by implementing interfaces.
Multiple Inheritance Achieved through interfaces, allowing a class to inherit behaviors from multiple sources.

When and Where to Use:

  • Interface: When you need to define a contract that multiple classes should adhere to but don’t want to specify how they implement it.
  • Polymorphism: When you want to write flexible and reusable code that works with multiple types of objects.
  • Multiple Inheritance: When a class needs to inherit behavior from more than one parent without the limitations of class-based inheritance.

3. Java Example: Interface with Polymorphism

In this example, we have a few key classes and interfaces:

  • Phone: An interface representing a generic phone.
  • Iphone: A class implementing the Phone interface.
  • SamsungPhone: Another class implementing the Phone interface.
  • Main: The main class where the program logic runs.

Main.java

4. Detailed Code Explanation

1. Phone Interface

2. Iphone Class

3. SamsungPhone Class

Step-by-Step Code Explanation

1. Main Class: The Main class is the entry point of the program, where the Iphone object is created. The methods processor(), spaceInGb(), and airdrop() are called to demonstrate the interface and polymorphism concepts.

2. Polymorphism: Since both Iphone and SamsungPhone implement the Phone interface, they can be used interchangeably in the program, allowing flexible behavior while adhering to a common contract.

5. Program Output and Explanation

Program Output

Explanation of the Output

  • A14 Bionic: This output comes from the processor() method of the Iphone class, which returns the processor type used by the iPhone.
  • 256: The output 256 is returned from the spaceInGb() method, representing the storage capacity of the iPhone.
  • true: The last output true comes from the airdrop() method, showing that the airdrop feature is available on the iPhone.

6. Conclusion

In this article, we covered the key concepts of interfaces, polymorphism, and multiple inheritance in Java, demonstrating their usage through an example project. The output of the program shows how interfaces allow for the abstraction of common methods, while the classes implementing the interface can have their own unique behaviors. This is a powerful feature in Java that promotes flexibility, reusability, and scalability in application development.