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()方法進行編譯時多態(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程序使用重寫方法進行編譯時多態(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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The yield keyword is used to create generators, generate values on demand, and save memory. 1. Replace return to generate finite sequences, such as Fibonacci sequences; 2. Implement infinite sequences, such as natural sequences; 3. Process big data or file readings, and process them line by line to avoid memory overflow; 4. Note that the generator can only traverse once, and can be called by next() or for loop.

VariableVariables is a feature in PHP that uses variable values as another variable name. It uses $$var to achieve dynamic access to variables, process form input, and build flexible configuration structures. For example, $name="age"; echo$$name is equivalent to the output value of $age; common usage scenarios include: 1. Dynamic access to variables, such as ${$type.'_info'}, different variables can be selected according to the conditions; 2. Automatically assign values when processing form input, but attention should be paid to security risks; 3. Build a flexible configuration structure and obtain corresponding values through string names; when using it, you need to pay attention to code maintenance, naming conflicts and debugging difficulties. It is recommended that only

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.

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.

PHP files are server-side scripting language files used for dynamic web development. They can process form data, connect to databases, generate dynamic content, and control access rights. It ends with .php, and the code returns the result to the browser after it is executed on the server. To run PHP files, you need to install a local server environment such as XAMPP, put the files in the server directory and access them through the browser. PHP is usually mixed with HTML. It is recommended to master HTML, CSS, JavaScript and basic programming concepts before learning. Practice more to get started quickly.

Common errors in using PHP variables include undefined variables, improper reference assignment, improper type comparison, and confusing global variables. 1. Ignoring that the undefined variable will cause an Notice error. You should use isset() or empty() to check; 2. Reference assignment modification will affect other variables, and you should clean up unset() after the loop; 3. Using == will lead to automatic conversion of types, and you should use === for congruent judgments; 4. Global variables are prone to confusion, and it is recommended to avoid or encapsulate them into class attributes to improve code clarity.

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.

There are four core points to develop a Go network scanner: 1. Select suitable libraries such as net and gopacket; 2. Understand the underlying protocols such as ICMP, TCP, SYN, and UDP; 3. Use goroutine and channel to design concurrency mechanisms and control quantity; 4. Ensure scanning compliance to avoid abuse. The basic methods of network scanning include ICMP detection host survival, TCP/SYN/UDP port detection, etc. Go's net library can implement basic scanning, and gopacket supports original packet operation. By limiting the number of goroutines, it can improve efficiency. Notes include legal authorization, rate control, and avoiding large-scale public network scanning
