Compile Time Polymorphism in Java


The polymorphism is a declaration of an object capacity present in a Java environment. It allows us to perform the same process in different manner. There are two types of polymorphism present in Java −

  • Compile-time polymorphism method

  • Run time polymorphism method

Today, we are going to discuss about the compile time polymorphism by using the Method overloading and Operator overloading.

Here is an example −

void ARBRDD() { ... }
void ARBRDD(int num1 ) { ... }
void ARBRDD(float num1) { ... }
void ARBRDD(int num1 , float num2 ) { ... }
show the value of ( char a )
show the value of ( char a ,char b )
show the value of ( float a float b)
show the value of ( int a, int b )
show the value of ( int a, float b )
show the value of ( float a, int b )
int sum value of (int, int);
String sum value of (int, int);

Algorithm to Perform Compile Time Polymorphism

In this possible algorithm, we are going to show you how to perform the compile time polymorphism in a Java envitonment. By using this algorithm, we will build some Java syntax, to explain the process in an efficient manner.

  • Step 1 − Start the process.

  • Step 2 − Import and declare the Java packages to run the method.

  • Step 3 − Declare a public class.

  • Step 4 − Mention the string argument.

  • Step 5 − Create And declare two function parameters.

  • Step 6 − Define the function parameter one.

  • Step 7 − Define the function parameter two.

  • Step 8 − Display both list.

  • Step 9 − Compare the both lists.

  • Step 10 − If it is a true evaluation, then print the equal message.

  • Step 11 − If it is a false evaluation, block the execution process and print the not equal text.

  • Step 12 − Insert one more element and override the method.

  • Step 13 − Display both.

  • Step 14 − Again compare both.

  • Step 15 − Get the results.

  • Step 16 − Terminate the process.

Syntax to Perform Compile Time Polymorphism

class SimpleCalculator{
	int add(int a, int b){
		return a+b;
	}
	int add(int a, int b, int c){
		return a+b+c;
	}
}
public class DemoCal{
	SimpleCalculator obj = new SimpleCalculator();
	System.out.println(obj.add(10, 20));
	System.out.println(obj.add(10, 20, 30));
}
}
class SimpleCalculator{
	int add(int a, int b){
		return a+b;
	}
	int add(int a, int b, int c){
		return a+b+c;
	}
}
public class DemoCal{
	SimpleCalculator obj = new SimpleCalculator();
	System.out.println(obj.add(10, 20));
	System.out.println(obj.add(10, 20, 30));
}
}
class MethodOverloading {
	private static void display(int a){
		System.out.println("Got Int data as a value.");
	}
	private static void display(String a){
		System.out.println("Got String object as a value.");
	}
	public static void main(String[] args) {
		display(4);
		display("XYZ");
	}
}
class Student{
	public void stuIdentity(String name, int id){
		System.out.println("stuName :" + name + " "
		+ "Id :" + id);
	}
	public void stuIdentity(int id, String name){
		System.out.println("Id :" + id + " " + "stuName :" + name);
	}
}
class Main {
	Student stu= new Student();
	stu.stuIdentity("Mohit Roy", 1);
	stu.stuIdentity(2, "Mohini Basu");
}
}

In this possible syntax above, we have tried to show youhow to build a function to use it in a method of polymorphism. By using these Java syntax we are heding towards some Java approaches related to the compile time polymorphism.

Approaches to Follow

  • Approach 1 − Java program to demonstrate the working of method overloading by changing the number of parameters to demonstrate compile time polymorphism

  • Approach 2 − Java program to the use of the render() type method for compile time polymorphism

Approach 1: to Perform the Compile Time Polymorphism by Using Number Parameters

Use of con_str Method

In this possible approach, we are going to apply the con_str method to demonstrate the working of compile time polymorphism by changing the number of parameters.

String con_str = s1 + s2;
System.out.println("Concatenated strings :"+ con_str);

Example

//Java program to demonstrate the working of method overloading by changing the number of parameters
public class ARBRDD {
   void show(int num1){
      System.out.println("number 1 : " + num1);
   }
   void show(int num1, int num2){
      System.out.println("number 1 : " + num1 + " number 2 : " + num2);
   }
   public static void main(String[] args){
      ARBRDD obj = new ARBRDD();
      obj.show(3);
      obj.show(4, 5);
   }
}

Output

number 1 : 3
number 1 : 4 number 2 : 5

Use of Datatype Method

In this possible approach, we are going to apply the data type schema method to demonstrate the working of compile time polymorphism by changing the number of parameters.

Example

//Java program to demonstrate the working of method overloading by changing the Datatype of parameter
public class ARBRDD {
   static void show(int a, int b){
      System.out.println("This is the integer function here");
   }
   static void show(double a, double b){
      System.out.println("This is the double function here");
   }
   public static void main(String[] args){
      show(1, 2);
      show(1.2, 2.4);
   }
}

Output

This is the integer function here
This is the double function here

Use of Sequence Parameters Method

In this possible approach, we are going to apply sequence parameters method to demonstrate the working of compile time polymorphism by changing the number of parameters.

Example

//Java program to demonstrate the working of method overloading by changing the sequence of parameters
public class ARBRDD {
   static void show(int a, char ch){
      System.out.println("integer : " + a + " and character : " + ch);
   }
   static void show(char ch, int a){
      System.out.println("character : " + ch + " and integer : " + a);
   }
   public static void main(String[] args){
      show(6, 'G');
      show('G', 7);
   }
}

Output

integer : 6 and character : G
character : G and integer : 7

Approach 2: Use of the Render() Method

In this possible approach,we are going to apply render method to explain the overloading with an operator by using the compile time polymorphism.

String s1 = sc.next();
System.out.println("Enter another string: ");
String s2 = sc.next();
System.out.println(s1+' '+s2);
System.out.println("Enter a number:");
int x = sc.nextInt();
System.out.println("Enter another number:");
int y = sc.nextInt();

Example 1

//Java program to the use of the render() method for compile time polymorphism
class Polygon {
   public void render() {
      System.out.println("Rendering Polygon Value...");
   }
}
class Square extends Polygon {
   public void render() {
      System.out.println("Rendering Square Value...");
   }
}
class Circle extends Polygon {
   public void render() {
      System.out.println("Rendering Circle Value...");
   }
}
public class ARBRDD {
   public static void main(String[] args) {
      Square s1 = new Square();
      s1.render();
      Circle c1 = new Circle();
      c1.render();
   }
}

Output

Rendering Square Value...
Rendering Circle Value...

In this possible approach,we are going to apply display information method to explain the overloading with an operator by using the compile time polymorphism.

Example 2

//Java program to the use of the overriding method for compile time polymorphism
class Language {
   public void displayInfo() {
      System.out.println("Common English Language");
   }
}
class Java extends Language {
   @Override
   public void displayInfo() {
      System.out.println("Java Programming Language");
   }
}
public class ARBRDD {
   public static void main(String[] args) {
      Java j1 = new Java();
      j1.displayInfo();
      Language l1 = new Language();
      l1.displayInfo();
   }
}

Output

Java Programming Language
Common English Language

In this possible approach,we are going to apply display() method to explain the overloading with an operator by using the compile time polymorphism.

Example 3

//Java program to the use of the class contains a method named display() for compile time polymorphism
class Pattern {
   public void display() {
      for (int i = 0; i < 10; i++) {
         System.out.print("@");
      }
   }
   public void display(char symbol) {
      for (int i = 0; i < 10; i++) {
         System.out.print(symbol);
      }
   }
}
public class ARBRDD {
   public static void main(String[] args) {
      Pattern d1 = new Pattern();
      d1.display();
      System.out.println("\n");
      d1.display('%');
   }
}

Output

@@@@@@@@@@
%%%%%%%%%%

In this possible approach,we are going to apply some polymorphic variables and method to explain the overloading with an operator by using the compile time polymorphism.

Example 4

//Java program to the use of the class contains a method named Polymorphic Variables for compile time polymorphism
class ProgrammingLanguage {
   public void display() {
      System.out.println("I am a programmer. I Am From India!");
   }
}
class Java extends ProgrammingLanguage {
   @Override
   public void display() {
      System.out.println("I am Java and I Am An Object-Oriented Programming Language.");
   }
}
public class ARBRDD {
   public static void main(String[] args) {
      ProgrammingLanguage pl;
      pl = new ProgrammingLanguage();
      pl.display();
      pl = new Java();
      pl.display();
   }
}

Output

I am a programmer. I Am From India!
I am Java and I Am An Object-Oriented Programming Language.

Conclusion

The compile time polymorphism is an early binding process by which we can resolve the overloading problems whrn a program is in the execution mode. Today in this article, we have learned about the various methods about the compile time polymorphism. By using the algorithm and syntax, we have also built some Java codes to explain the problem statement in an efficient manner.

Updated on: 29-Dec-2023

57 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements