Interfaces and Polymorphism in Java
Last Updated :
02 Sep, 2021
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:
- Run time polymorphism
- 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
import java.io.*;
class GFG1 {
void name() {
System.out.println( "This is the GFG1 class" );
}
}
public class GFG extends GFG1 {
void name() {
System.out.println( "This is the GFG class" );
}
public static void main(String[] args) {
GFG1 ob = new GFG1();
ob.name();
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
import java.io.*;
import java.util.*;
class First {
void check()
{
System.out.println( "This is the class First" );
}
}
class Second extends First {
void check(String name)
{
System.out.println( "This is the class " + name);
}
public static void main(String args[])
{
Second ob = new Second();
ob.check( "Second" );
First ob1 = new First();
ob.check();
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
interface salary {
void insertsalary( int salary);
}
class SDE1 implements salary {
int salary;
@Override public void insertsalary( int salary)
{
this .salary = salary;
}
void printSalary() { System.out.println( this .salary); }
}
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();
ob.insertsalary( 100000 );
ob.printSalary();
SDE2 ob1 = new SDE2();
ob1.insertsalary( 200000 );
ob1.printSalary();
}
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...