Interface with Polymorphism and Multiple Inheritance in Java
Table of Contents
- Introduction
- Key Concepts of Interface, Polymorphism, and Multiple Inheritance
- Java Example: Interface with Polymorphism
- Detailed Code Explanation
- Program Output and Explanation
- Conclusion
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
1 2 3 4 5 6 7 8 9 10 11 |
package org.studyeasy; public class Main { public static void main(String[] args) { Iphone phone = new Iphone(); String p = phone.processor(); System.out.println(p); System.out.println(phone.spaceInGb()); System.out.println(phone.airdrop()); } } |
4. Detailed Code Explanation
1. Phone Interface
1 2 3 4 |
public interface Phone { String processor(); int spaceInGb(); } |
2. Iphone Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Iphone implements Phone { @Override public String processor() { return "A14 Bionic"; } @Override public int spaceInGb() { return 256; } public boolean airdrop() { return true; } } |
3. SamsungPhone Class
1 2 3 4 5 6 7 8 9 10 11 |
public class SamsungPhone implements Phone { @Override public String processor() { return "Exynos 2100"; } @Override public int spaceInGb() { return 512; } } |
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
1 2 3 |
A14 Bionic 256 true |
Explanation of the Output
- A14 Bionic: This output comes from the
processor()
method of theIphone
class, which returns the processor type used by the iPhone. - 256: The output
256
is returned from thespaceInGb()
method, representing the storage capacity of the iPhone. - true: The last output
true
comes from theairdrop()
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.