Interfaces and Polymorphism in Java


Let's start this article by discussing how Java defines the terms Interface and Polymorphism and later, we will dive deep into these concepts with the help of programming examples. Polymorphism, as the name illustrates it means many forms, is the ability of an object to take different forms depending on the context. An Interface is a type of class that is defined using the keyword 'interface' and has only method bodies without any implementations.

Polymorphism via Interface in Java

In this section, we will guide how to implement polymorphism using interfaces. But before that, it is important to have a proper understanding of these concepts.

Polymorphism

In polymorphism, we can refer to a child class by using the parent class reference. Hence, that object has many forms: the parent class reference and also the child class reference. The two most popular examples of polymorphism are method overloading which is also called as static polymorphism and the other one is overriding, a runtime polymorphism.

Example

In this example, we will show how compile time polymorphism works in Java. We will create two methods with the same name but with different signatures. The method of child class will overload the method of parent class. Method overloading is called as compile time polymorphism because the compiler determines the method call during compilation depending on the given arguments.

class Example1 { // parent class
   public void method() { 
      System.out.println("Tutorialspoint");
   }
}
// creating a child class
public class Example2 extends Example1 {
   public void method(int id) { // method overloading
      System.out.println("ID: " + id);
   }
   public static void main(String []args){
      // object creation of the classes
      Example1 exp1 = new Example1();
      Example2 exp2 = new Example2();
      // method calling
      exp1.method(); 
      exp2.method(125);
   }
}

Output

Tutorialspoint
ID: 125

Interface

In Java, interfaces serve two purposes pure abstraction and multiple inheritance. Generally, an interface consists of abstract methods and variables that define the behavior which a class can implement. Other than abstract methods and variables, an interface can also contain default methods, constants, and static methods. To create an interface, we use the keyword 'interface' and to access its members within a class, we need to use the 'implements' keyword while defining that class.

Syntax

interface nameOfInterface {
   method1();
   method2();
}

Java Program to Implement Polymorphism using Interface

Let's discuss the approach first.

Approach

  • The first step is to create an Interface with a method declaration only.

  • Then, create two classes implementing the interface and overriding its method to print different message.

  • In the main() method, define an instance of the interface and using this instance, create instances of both classes.

  • Now, call the methods using those instances and exit.

Example

interface Message { // creating an interface 
   public void method(String name); // method declaration
}
// class implementing interface
class Example1 implements Message { 
   String name;
   public void method(String name) { 
      System.out.println("Hello " + name);
   }
}
// another class implementing interface
class Example2 implements Message {
   String name;
   public void method(String name) { 
      System.out.println("Hello " + name);
   }
}
public class PolyExample {
   public static void main(String []args){
      // creating an instance of interface
      Message msg;
      // creating instances and assigning them to interface reference
      msg = new Example1();
      msg.method("Ram"); // method call
      msg = new Example2();
      msg.method("Shyam");  // method call
   }
}

Output

Hello Ram
Hello Shyam

Conclusion

We started this article by defining polymorphism and interface. In the next section, we explained them in detail with example programs. Also, we discovered method overloading and overriding. In the end, we discussed a Java program that demonstrates how to implement Polymorphism using Interface.

Updated on: 17-Aug-2023

382 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements