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:
Today, we will discuss compile-time polymorphisms using method overloading and operator overloading.
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);
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.
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.
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);
//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); } }
<code>number 1 : 3 number 1 : 4 number 2 : 5</code>
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.
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);
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 this method, we will apply the sequence parameter 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);
//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); } }
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>
//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); } }
<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.
//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); } }
<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.
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();
//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.
<code>Rendering Square Value... Rendering Circle Value...</code>
//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(); } }
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!