Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S07L03 - Interface with Polymorphism and multiple inheritance

Java Articles
1 views

S07L03 - Interface with Polymorphism and multiple inheritance


February 13, 2025

Interface with Polymorphism and Multiple Inheritance in Java: A Beginner’s eBook Guide

Note: This article is AI generated.

Table of Contents

1. Introduction ................................................................ 3
2. Understanding Interfaces in Java ......................................... 4
 2.1 What is an Interface? ................................................... 4
 2.2 Importance of Using Interfaces .......................................... 5
3. Polymorphism and Interfaces: Concepts and Applications ................. 6
 3.1 How Polymorphism Works with Interfaces ............................... 6
 3.2 Access Specifiers in Interfaces ......................................... 7
4. Implementing Multiple Inheritance Using Interfaces ....................... 8
 4.1 Creating Multiple Interfaces (Android & IOS) ........................... 8
 4.2 Combining Interfaces in Concrete Classes .............................. 9
5. Code Walkthrough and Explanation ......................................... 10
 5.1 Sample Code with Comments ............................................ 10
 5.2 Step-by-Step Explanation and Output ................................. 11
6. Diagram and Comparison Tables ............................................ 12
 6.1 UML Diagram and Conceptual Overview ................................. 12
 6.2 Comparison of Phone Implementations ................................... 13
7. Conclusion .................................................................. 14


1. Introduction

Java is renowned for its powerful object-oriented features, and one key concept is the use of interfaces. This eBook explores how interfaces enable polymorphism and simulate multiple inheritance in Java. We discuss why and when to use interfaces, the benefits of restricting method availability via interfaces, and the nuances of access specifiers. The article includes clear code samples (taken directly from the project files), detailed explanations, a diagnostic diagram, and comparison tables that will help beginners and developers gain a sound foundation in this topic.

Here’s an overview table of the content covered in this guide:

Topic Focus When/Where to Use
Java Interfaces Abstraction, defining contracts For method signature control
Polymorphism with Interfaces Dynamic method access To restrict object functionality
Multiple Inheritance Combining multiple interfaces For added functionality without class inheritance

2. Understanding Interfaces in Java

What is an Interface?

An interface in Java defines a contract that classes can implement. It outlines method signatures without providing complete method implementations. This allows developers to ensure that all classes agreeing to the interface will have specific functionalities.

Importance of Using Interfaces

Interfaces enforce a set of constraints on classes—guaranteeing that certain methods are present. For example, when a class implements a Phone-like interface, you are assured that it has a “call” method. This makes it easier to work with different implementations (such as SamsungPhone or iPhone) without worrying about missing functionality.


3. Polymorphism and Interfaces: Concepts and Applications

How Polymorphism Works with Interfaces

Polymorphism in Java allows objects of different classes to be treated as objects of a common supertype. Using interfaces, you can assign an object of a concrete class (e.g., SamsungPhone) to a variable of an interface type (e.g., Phone). This increases flexibility since you can switch the implementation without changing the interface usage in your code.

Access Specifiers in Interfaces

One key point is that although methods in interfaces are inherently public, explicitly using the public modifier is optional. In Java interfaces, specifying access as private or protected is not allowed. This ensures that any class implementing the interface can access and override the declared methods.


4. Implementing Multiple Inheritance Using Interfaces

While Java does not support multiple inheritance with classes, it achieves similar functionality using multiple interfaces. For instance, we create two separate interfaces to simulate different features:

Creating Multiple Interfaces (Android & IOS)

• The Android interface might declare a method like WhatsApp(), unique to SamsungPhone implementations.
• The IOS interface might declare a method like airDrop(), a feature specific to iPhone.

Combining Interfaces in Concrete Classes

By having SamsungPhone implement both Phone and Android, and iPhone implement Phone and IOS, we can deliver specialized implementations while still maintaining a common behavior based on the Phone interface. However, note that when using a reference with type Phone, only the methods defined in Phone will be accessible. Casting to the proper interface type is required for accessing more specific methods.


5. Code Walkthrough and Explanation

Below is the sample code deriving from the project files. This code demonstrates polymorphism and the concept of multiple inheritance through interfaces.

Sample Code with Comments

/* Interface defining basic phone functionality */
public interface Phone {
    void call();   // Method to perform a call
}

/* Android interface extending Phone for Android-specific feature */
public interface Android extends Phone {
    // Method to simulate WhatsApp functionality
    String WhatsApp();  
}

/* IOS interface extending Phone for IOS-specific feature */
public interface IOS extends Phone {
    // Method to simulate AirDrop functionality
    String airDrop();  
}

/* SamsungPhone implements Phone and Android interfaces */
public class SamsungPhone implements Android {
    // Implementation of call() method
    public void call() { 
        System.out.println("SD 100512 from Samsung phone");
    }
    
    // Implementation of WhatsApp() method specific to Android
    public String WhatsApp() { 
        return "Send messages for free";
    }
}

/* iPhone implements Phone and IOS interfaces */
public class iPhone implements IOS {
    // Implementation of call() method
    public void call() { 
        System.out.println("SD 100512 from iPhone");
    }
    
    // Implementation of airDrop() method specific to iOS
    public String airDrop() { 
        return "AirDrop activated";
    }
}

/* Main class to drive the demonstration */
public class Main {
    public static void main(String[] args) {
        // Creating a SamsungPhone object using the Phone interface type
        Phone phone = new SamsungPhone();
        phone.call();   // Calls SamsungPhone's call method
        
        // To access methods unique to the Android interface, cast is required
        Android androidPhone = (Android) phone;
        System.out.println(androidPhone.WhatsApp());
        
        // Similarly, if using iPhone, declare using iPhone reference to access airDrop()
        iPhone myIphone = new iPhone();
        myIphone.call();  // Calls iPhone's call method
        System.out.println(myIphone.airDrop());
    }
}

Step-by-Step Explanation and Output

1. The Phone interface declares a call() method, ensuring that any phone implementation includes this functionality.
2. Android and IOS are additional interfaces, each declaring their unique methods — WhatsApp() and airDrop(), respectively.
3. SamsungPhone implements both Phone and Android. When using a reference of type Phone, only the call() method is directly accessible. To use WhatsApp(), a cast to Android is required.
4. iPhone implements Phone and IOS, providing its own version of call() and airDrop().

Expected program output when running Main:

SD 100512 from Samsung phone
Send messages for free
SD 100512 from iPhone
AirDrop activated

6. Diagram and Comparison Tables

UML Diagram and Conceptual Overview

Below is a conceptual UML diagram represented textually:

   [Phone Interface]
         ↑
         | (implements)
[SamsungPhone]          [iPhone]
         |                   |
  Implements Android    Implements IOS
         |                   |
  (WhatsApp method)     (airDrop method)

Comparison of Phone Implementations

Model Implemented Interfaces Unique Methods
SamsungPhone Phone, Android WhatsApp(): Returns a message string
iPhone Phone, IOS airDrop(): Returns a message string

7. Conclusion

In this eBook–style article, we delved into the use of interfaces in Java, examined how they can be used to realize polymorphism and simulate multiple inheritance, and explored the practical side of their implementation through clear sample code and diagrams. The demonstration provided a nuanced look at the differences between interface types and explained why casting is essential when accessing features defined outside a common interface. By practicing these concepts, you can advance as a proficient Java developer.

SEO Optimized Keywords: Java interfaces, polymorphism, multiple inheritance, Java programming, SamsungPhone, iPhone, Android API, iOS development, object-oriented programming, Java tutorials, beginner Java, technical writing, eBook guide

This concludes our detailed and SEO-optimized guide on using interfaces with polymorphism and multiple inheritance in Java. Happy coding!

Share Article

Back to Blog

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.