国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Compiled-time polymorphism example
Algorithm for executing compile-time polymorphism
Syntax for executing compile-time polymorphism
Methods to follow
Method 1: Use numerical parameters to perform compile-time polymorphisms
Usage of con_str method
Example 4
Output
Usage of data type methods
Using of sequence parameter methods
Method 2: Use of render() method
Conclusion
Home Java javaTutorial Compile Time Polymorphism in Java

Compile Time Polymorphism in Java

Feb 07, 2025 am 11:39 AM
java

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);

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

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);

Example

//Java程序演示通過更改參數(shù)數(shù)量來演示編譯時(shí)多態(tài)性的方法重載的工作原理
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

<code>number 1 : 3
number 1 : 4 number 2 : 5</code>

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);

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

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);

Output

//Java程序演示通過更改參數(shù)數(shù)量來演示編譯時(shí)多態(tài)性的方法重載的工作原理
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);
   }
}

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>

Example 1

//Java程序演示通過更改參數(shù)的數(shù)據(jù)類型來演示方法重載的工作原理
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

<code>This is the integer function here
This is the double function here</code>

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

Example 2

//Java程序演示通過更改參數(shù)的順序來演示方法重載的工作原理
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

<code>integer : 6 and character : G
character : G and integer : 7</code>

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();

Output

//Java程序使用render()方法進(jìn)行編譯時(shí)多態(tài)性
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();
   }
}

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>

Output

//Java程序使用重寫方法進(jìn)行編譯時(shí)多態(tài)性
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();
   }
}

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!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for Writing PHP Comments Tips for Writing PHP Comments Jul 18, 2025 am 04:51 AM

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

Writing Effective PHP Comments Writing Effective PHP Comments Jul 18, 2025 am 04:44 AM

Comments cannot be careless because they want to explain the reasons for the existence of the code rather than the functions, such as compatibility with old interfaces or third-party restrictions, otherwise people who read the code can only rely on guessing. The areas that must be commented include complex conditional judgments, special error handling logic, and temporary bypass restrictions. A more practical way to write comments is to select single-line comments or block comments based on the scene. Use document block comments to explain parameters and return values at the beginning of functions, classes, and files, and keep comments updated. For complex logic, you can add a line to the previous one to summarize the overall intention. At the same time, do not use comments to seal code, but use version control tools.

Improving Readability with Comments Improving Readability with Comments Jul 18, 2025 am 04:46 AM

The key to writing good comments is to explain "why" rather than just "what was done" to improve the readability of the code. 1. Comments should explain logical reasons, such as considerations behind value selection or processing; 2. Use paragraph annotations for complex logic to summarize the overall idea of functions or algorithms; 3. Regularly maintain comments to ensure consistency with the code, avoid misleading, and delete outdated content if necessary; 4. Synchronously check comments when reviewing the code, and record public logic through documents to reduce the burden of code comments.

Effective PHP Commenting Effective PHP Commenting Jul 18, 2025 am 04:33 AM

The key to writing PHP comments is clear, useful and concise. 1. Comments should explain the intention behind the code rather than just describing the code itself, such as explaining the logical purpose of complex conditional judgments; 2. Add comments to key scenarios such as magic values, old code compatibility, API interfaces, etc. to improve readability; 3. Avoid duplicate code content, keep it concise and specific, and use standard formats such as PHPDoc; 4. Comments should be updated synchronously with the code to ensure accuracy. Good comments should be thought from the perspective of others, reduce the cost of understanding, and become a code understanding navigation device.

PHP Development Environment Setup PHP Development Environment Setup Jul 18, 2025 am 04:55 AM

The first step is to select the integrated environment package XAMPP or MAMP to build a local server; the second step is to select the appropriate PHP version according to the project needs and configure multiple version switching; the third step is to select VSCode or PhpStorm as the editor and debug with Xdebug; in addition, you need to install Composer, PHP_CodeSniffer, PHPUnit and other tools to assist in development.

Exploring Basic PHP Syntax Exploring Basic PHP Syntax Jul 17, 2025 am 04:11 AM

The basic PHP syntax includes: 1. Use wrapping code; 2. Use echo or print to output content, where echo supports multiple parameters; 3. Variables do not need to declare types, start with $. Common types include strings, integers, floating-point numbers, booleans, arrays and objects. Mastering these key points can help you get started with PHP development quickly.

Understanding PHP Variables Understanding PHP Variables Jul 17, 2025 am 04:11 AM

PHP variables start with $, and the naming must follow rules, such as they cannot start with numbers and are case sensitive; the scope of the variable is divided into local, global and hyperglobal; global variables can be accessed using global, but it is recommended to pass them with parameters; mutable variables and reference assignments should be used with caution. Variables are the basis for storing data, and correctly mastering their rules and mechanisms is crucial to development.

Understanding PHP Variable Types Understanding PHP Variable Types Jul 17, 2025 am 04:12 AM

PHP has 8 variable types, commonly used include Integer, Float, String, Boolean, Array, Object, NULL and Resource. To view variable types, use the gettype() or is_type() series functions. PHP will automatically convert types, but it is recommended to use === to strictly compare the key logic. Manual conversion can be used for syntax such as (int), (string), etc., but be careful that information may be lost.

See all articles