Monday, November 18, 2024
Google search engine
HomeLanguagesJavaInterfaces and Polymorphism in Java

Interfaces and Polymorphism in Java

Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using JA is that Java tries to connect every concept in the language to the real world with the help of the concepts of classes, inheritance, polymorphism, interfaces, etc. In this article, we will discuss polymorphism and interface concepts.

Polymorphism is that it has many forms that mean one specific defined form is used in many different ways. The simplest real-life example is let’s suppose we have to store the name of the person and the phone number of the person, but there are many situations when a person has two different phone numbers. We have to save the same phone number under the same name.

Let us interpret it with help . So, in java, the problem can be solved using an object-oriented concept, void insertPhone(String name, int phone). So, this method is used to save the phone number of the particular person. Similarly, we can use the same form but a different signature means different parameters to store the alternative phone number of the person’s void insertPhone(String name, int phone1, int phone2). One method has two different forms and performs different operations. This is an example of polymorphism, which is method overloading.

Types of polymorphism in Java:

  1. Run time polymorphism
  2. Compile-time polymorphism

Type 1: Run time polymorphism

This type of polymorphism is resolved by the java virtual machine, not by the java compiler. That’s why this type of polymorphism is called run-time polymorphism. Run time polymorphism occurs during method overriding in java.

Example 

Java




// Java Program to Illustrate Run-time polymorphism
 
// Importing I/O classes
import java.io.*;
 
// Class 1 (Parent class)
class GFG1 {
  //name method
  void name() {
    System.out.println("This is the GFG1 class");
  }
}
 
// Class 2 (Child class)
// Main class extending parent class
public class GFG extends GFG1 {
 
  // Method 1
  void name() {
    // Print statement
    System.out.println("This is the GFG class");
  }
 
  // Method 2
  // Main drive method
  public static void main(String[] args) {
 
    // Now creating 2 objects with different references and
    // calling the Method 1 over the objects
 
    // Case 1: GFG1 reference and GFG1 is the object
    GFG1 ob = new GFG1();
    ob.name();
 
    // Case 2: GFG1 reference and GFG is the object
    GFG1 ob1 = new GFG();
    ob1.name();
  }
}


Output

This is the GFG1 class
This is the GFG class

Output explanation: 

In the above example, the same function i.e name is called two times, but in both cases, the output is different. The signatures of these methods are also the same. That’s why compilers cannot be able to identify which should be executed. This is determined only after the object creation and reference of the class, which is performed during run time (Memory management ). That’s why this is run-time polymorphism.

Type 2: Compile-time polymorphism

Method overloading is an example of the compile-time polymorphism method. Overloading means a function having the same name but a different signature. This is compile-time polymorphism because this type of polymorphism is determined during the compilation time because during writing the code we already mention the different types of parameters for the same function name.

Example:

Java




// Java Program to Illustrate Run-time polymorphism
 
// Importing required classes
import java.io.*;
import java.util.*;
 
// Class 1
// Helper class
class First {
 
    // Method of this class
    // Without any parameter
    void check()
    {
 
        // Print statement if this method is called
        System.out.println("This is the class First");
    }
}
 
// Class 2
// Main class
class Second extends First {
 
    // Method overloading
    void check(String name)
    {
        // Printing the name of the class method having the
        // parameter
        System.out.println("This is the class " + name);
    }
 
    // Method 2
    // Main driver method
    public static void main(String args[])
    {
        // Creating object of class 2
        Second ob = new Second();
        // Calling method over class 2 object
        ob.check("Second");
 
        // Creating object of class 1
        First ob1 = new First();
        ob.check();
 
        // Upcasting
        First ob2 = new Second();
        ob.check();
    }
}


Output

This is the class Second
This is the class First
This is the class First

Interfaces are very similar to classes. They have variables and methods but the interfaces allow only abstract methods(that don’t contain the body of the methods), but what is the difference between the classes and the interfaces? The first advantage is to allow interfaces to implement the multiple inheritances in a particular class. The JAVA language doesn’t support multiple inheritances if we extend multiple classes in the class, but with the help of the interfaces, multiple inheritances are allowed in Java.

Real-life Example

The real-world example of interfaces is that we have multiple classes for different levels of employees working in a particular company and the necessary property of the class is the salary of the employees and this. We must be implemented in every class and. Also, it is different for every employee here. The concept of the interface is used. We simply create an interface containing an abstract salary method and implement it in all the classes and we can easily define different salaries of the employees.

Example:

Java




// Java Program to Demonstrate Concept of interfaces
 
// Interface
interface salary {
    void insertsalary(int salary);
}
 
// Class 1
// Implementing the salary in the class
class SDE1 implements salary {
    int salary;
    @Override public void insertsalary(int salary)
    {
        this.salary = salary;
    }
    void printSalary() { System.out.println(this.salary); }
}
 
// Class 2
// Implementing the salary inside the SDE2 class
class SDE2 implements salary {
    int salary;
    @Override public void insertsalary(int salary)
    {
        this.salary = salary;
    }
    void printSalary() { System.out.println(this.salary); }
}
 
public class GFG {
 
    public static void main(String[] args)
    {
        SDE1 ob = new SDE1();
        // Insert different salaries
        ob.insertsalary(100000);
        ob.printSalary();
        SDE2 ob1 = new SDE2();
 
        ob1.insertsalary(200000);
        ob1.printSalary();
    }
}


Output

100000
200000
RELATED ARTICLES

Most Popular

Recent Comments