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程序演示通过更改参数数量来演示编译时多态性的方法重载的工作原理 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程序演示通过更改参数数量来演示编译时多态性的方法重载的工作原理 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程序演示通过更改参数的数据类型来演示方法重载的工作原理 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程序演示通过更改参数的顺序来演示方法重载的工作原理 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()方法进行编译时多态性 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程序使用重写方法进行编译时多态性 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

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

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

Undress AI Tool
Undress images for free

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

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
