国产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ù)量來演示編譯時多態(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ù)量來演示編譯時多態(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)行編譯時多態(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)行編譯時多態(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)

What is a Singleton design pattern in Java? What is a Singleton design pattern in Java? Jul 09, 2025 am 01:32 AM

Singleton design pattern in Java ensures that a class has only one instance and provides a global access point through private constructors and static methods, which is suitable for controlling access to shared resources. Implementation methods include: 1. Lazy loading, that is, the instance is created only when the first request is requested, which is suitable for situations where resource consumption is high and not necessarily required; 2. Thread-safe processing, ensuring that only one instance is created in a multi-threaded environment through synchronization methods or double check locking, and reducing performance impact; 3. Hungry loading, which directly initializes the instance during class loading, is suitable for lightweight objects or scenarios that can be initialized in advance; 4. Enumeration implementation, using Java enumeration to naturally support serialization, thread safety and prevent reflective attacks, is a recommended concise and reliable method. Different implementation methods can be selected according to specific needs

What is a ThreadLocal in Java? What is a ThreadLocal in Java? Jul 09, 2025 am 02:25 AM

ThreadLocal is used in Java to create thread-private variables, each thread has an independent copy to avoid concurrency problems. It stores values ??through ThreadLocalMap inside the thread. Pay attention to timely cleaning when using it to prevent memory leakage. Common uses include user session management, database connections, transaction context, and log tracking. Best practices include: 1. Call remove() to clean up after use; 2. Avoid overuse; 3. InheritableThreadLocal is required for child thread inheritance; 4. Do not store large objects. The initial value can be set through initialValue() or withInitial(), and the initialization is delayed until the first get() call.

How to analyze a Java heap dump? How to analyze a Java heap dump? Jul 09, 2025 am 01:25 AM

Analyzing Java heap dumps is a key means to troubleshoot memory problems, especially for identifying memory leaks and performance bottlenecks. 1. Use EclipseMAT or VisualVM to open the .hprof file. MAT provides Histogram and DominatorTree views to display the object distribution from different angles; 2. sort in Histogram by number of instances or space occupied to find classes with abnormally large or large size, such as byte[], char[] or business classes; 3. View the reference chain through "ListObjects>withincoming/outgoingreferences" to determine whether it is accidentally held; 4. Use "Pathto

Java Optional example Java Optional example Jul 12, 2025 am 02:55 AM

Optional can clearly express intentions and reduce code noise for null judgments. 1. Optional.ofNullable is a common way to deal with null objects. For example, when taking values ??from maps, orElse can be used to provide default values, so that the logic is clearer and concise; 2. Use chain calls maps to achieve nested values ??to safely avoid NPE, and automatically terminate if any link is null and return the default value; 3. Filter can be used for conditional filtering, and subsequent operations will continue to be performed only if the conditions are met, otherwise it will jump directly to orElse, which is suitable for lightweight business judgment; 4. It is not recommended to overuse Optional, such as basic types or simple logic, which will increase complexity, and some scenarios will directly return to nu.

How to fix java.io.NotSerializableException? How to fix java.io.NotSerializableException? Jul 12, 2025 am 03:07 AM

The core workaround for encountering java.io.NotSerializableException is to ensure that all classes that need to be serialized implement the Serializable interface and check the serialization support of nested objects. 1. Add implementsSerializable to the main class; 2. Ensure that the corresponding classes of custom fields in the class also implement Serializable; 3. Use transient to mark fields that do not need to be serialized; 4. Check the non-serialized types in collections or nested objects; 5. Check which class does not implement the interface; 6. Consider replacement design for classes that cannot be modified, such as saving key data or using serializable intermediate structures; 7. Consider modifying

How to implement a caching strategy in Java (e.g., using EhCache or Caffeine)? How to implement a caching strategy in Java (e.g., using EhCache or Caffeine)? Jul 09, 2025 am 01:17 AM

ToimproveperformanceinJavaapplications,choosebetweenEhCacheandCaffeinebasedonyourneeds.1.Forlightweight,modernin-memorycaching,useCaffeine—setitupbyaddingthedependency,configuringacachebeanwithsizeandexpiration,andinjectingitintoservices.2.Foradvance

How to parse JSON in Java? How to parse JSON in Java? Jul 11, 2025 am 02:18 AM

There are three common ways to parse JSON in Java: use Jackson, Gson, or org.json. 1. Jackson is suitable for most projects, with good performance and comprehensive functions, and supports conversion and annotation mapping between objects and JSON strings; 2. Gson is more suitable for Android projects or lightweight needs, and is simple to use but slightly inferior in handling complex structures and high-performance scenarios; 3.org.json is suitable for simple tasks or small scripts, and is not recommended for large projects because of its lack of flexibility and type safety. The choice should be decided based on actual needs.

How to handle serialization and deserialization in Java? How to handle serialization and deserialization in Java? Jul 09, 2025 am 01:49 AM

Serialization is the process of converting an object into a storageable or transferable format, while deserialization is the process of restoring it to an object. Implementing the Serializable interface in Java can use ObjectOutputStream and ObjectInputStream to operate. 1. The class must implement the Serializable interface; 2. All fields must be serializable or marked as transient; 3. It is recommended to manually define serialVersionUID to avoid version problems; 4. Use transient to exclude sensitive fields; 5. Rewrite readObject/writeObject custom logic; 6. Pay attention to security, performance and compatibility

See all articles