Home > Java > javaTutorial > Compile Time Polymorphism in Java

Compile Time Polymorphism in Java

DDD
Release: 2025-02-07 11:39:14
Original
1041 people have browsed it

Compile Time Polymorphism in Java

Polymorphism in Java refers to a capability declaration of objects in the Java environment. It allows us to perform the same process in different ways. There are two types of polymorphisms in Java:

  • Compiled polymorphism method
  • Runtime Polymorphism Method

Today, we will discuss compile-time polymorphisms using method overloading and operator overloading.

Compiled-time polymorphism example

This is an example:

void ARBRDD() { ... }
void ARBRDD(int num1 ) { ... }
void ARBRDD(float num1) { ... }
void ARBRDD(int num1 , float num2 ) { ... }
//显示(char a)的值
//显示(char a, char b)的值
//显示(float a, float b)的值
//显示(int a, int b)的值
//显示(int a, float b)的值
//显示(float a, int b)的值
int sum value of (int, int);
String sum value of (int, int);
Copy after login
Copy after login

Algorithm for executing compile-time polymorphism

In this possible algorithm, we will show you how to perform compile-time polymorphisms in a Java environment. By using this algorithm, we will build some Java syntax to interpret the process in an efficient way.

  • Step 1 - Start the process.
  • Step 2 - Import and declare the Java package used to run the method.
  • Step 3 - Declare a public class.
  • Step 4 - mentions string parameters.
  • Step 5 - Create and declare two function parameters.
  • Step 6 - Define function parameter 1.
  • Step 7 - Define function parameter two.
  • Step 8 - Show two lists.
  • Step 9 - Compare two lists.
  • Step 10 - If the evaluation result is true, an equal message is printed.
  • Step 11 - If the evaluation result is false, the execution of the process is blocked and unequal text is printed.
  • Step 12 - Insert another element and overwrite the method.
  • Step 13 - Show both.
  • Step 14 - Compare the two again.
  • Step 15 - Get the results.
  • Step 16 - Terminate the process.

Syntax for executing 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");
}
}
Copy after login
Copy after login

In the syntax above, we try to show you how to build a function to use it in a polymorphic method. By using these Java syntaxes, we will move towards some Java methods related to compile-time polymorphism.

Methods to follow

  • Method 1 - Java program demonstrates how method overloading works when compiling polymorphism by changing the number of parameters
  • Method 2 - Java programs use render() type method for compile-time polymorphism

Method 1: Use numerical parameters to perform compile-time polymorphisms

Usage of con_str method

In this method, we will apply the con_str method to demonstrate how polymorphism works at compile time by changing the number of parameters.

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

Example

//Java程序演示通过更改参数数量来演示编译时多态性的方法重载的工作原理
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);
   }
}
Copy after login
Copy after login

Output

<code>number 1 : 3
number 1 : 4 number 2 : 5</code>
Copy after login
Copy after login

Usage of data type methods

In this method, we will apply the data type pattern method to demonstrate how polymorphism works at compile time by changing the number of parameters.

Example

void ARBRDD() { ... }
void ARBRDD(int num1 ) { ... }
void ARBRDD(float num1) { ... }
void ARBRDD(int num1 , float num2 ) { ... }
//显示(char a)的值
//显示(char a, char b)的值
//显示(float a, float b)的值
//显示(int a, int b)的值
//显示(int a, float b)的值
//显示(float a, int b)的值
int sum value of (int, int);
String sum value of (int, int);
Copy after login
Copy after login

Output

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");
}
}
Copy after login
Copy after login

Using of sequence parameter methods

In this method, we will apply the sequence parameter method to demonstrate how polymorphism works at compile time by changing the number of parameters.

Example

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

Output

//Java程序演示通过更改参数数量来演示编译时多态性的方法重载的工作原理
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);
   }
}
Copy after login
Copy after login

Method 2: Use of render() method

In this method, we will apply the render method to explain operator overloading using compile-time polymorphism.

<code>number 1 : 3
number 1 : 4 number 2 : 5</code>
Copy after login
Copy after login

Example 1

//Java程序演示通过更改参数的数据类型来演示方法重载的工作原理
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);
   }
}
Copy after login

Output

<code>This is the integer function here
This is the double function here</code>
Copy after login

In this method, we will apply the display information method to interpret operator overloading using compile-time polymorphism.

Example 2

//Java程序演示通过更改参数的顺序来演示方法重载的工作原理
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);
   }
}
Copy after login

Output

<code>integer : 6 and character : G
character : G and integer : 7</code>
Copy after login

In this method, we will apply the display() method to explain operator overloading using compile-time polymorphism.

Example 3

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();
Copy after login

Output

//Java程序使用render()方法进行编译时多态性
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();
   }
}
Copy after login

In this method, we will apply some polymorphic variables and methods to explain operator overloading using compile-time polymorphism.

Example 4

<code>Rendering Square Value...
Rendering Circle Value...</code>
Copy after login

Output

//Java程序使用重写方法进行编译时多态性
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();
   }
}
Copy after login

Conclusion

Compilation-time polymorphism is an early binding process, through which we can solve the overloading problem that a program occurs in execution mode. In today's article, we learn various methods about compile-time polymorphism. By using algorithms and syntax, we also built some Java code to interpret problem statements in an efficient way.

Please read also: Java interview questions and answers

The code examples have been improved for clarity and correctness, and the text has been rewriteten to be more concise and engaging while maintaining the original m eaning. The image remains in its original format and location.

The above is the detailed content of Compile Time Polymorphism in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template