Compile-time Polymorphism

I have a question about compile-time polymorphism in java.

Is the method an overload of the way to achieve compile-time polymorphism? If so, is this the only way? A small example helped me a lot.

I searched on the Internet and different sources give different answers and confuse. That is why I thought to ask about it here.

Thanks in advance.

+5
source share
4 answers

I found this external source . This makes the claim that there is no “compilation time polymorphism”. Do you mean run-time polymorphism?

In fact, polymorphism refers to the sign that the method is not executed by the implementation of the fixed method, which is determined at compile time, but there is a search at run time, the selected implementation of the method for making the call.

For example, there is an Object :: equals object in Java that has an implementation in the "Object" class. If you create your own class that has its own implementation of the "equals" method, this implementation will be selected when comparing the instances, and not with the implementation defined in the "Object" class.

Polymorphism becomes very convenient when the complete list of method implementations is unknown, for example. because you provide a library that is used in programs / other libraries that can declare their own (derived) classes that implement this method.

+3
source

Consider this example

interface Animal { public void move(location); } Animal[] noahsArk = new Animal[...]; for (Animal animal : allAnimals) { move(arkLocation); } 

Since Dogs, Cats, Ants, etc. - all Animals, we can move (...) them all together, without worrying about the details of the movement of each of them.

+2
source

In short, the compiler knows about static polymorphism because all methods of the same class are different from each other, even if they have the same name, the difference lies in their method signatures.

To execute a java program, (java class), we need to compile (using the Javac compiler) and execute a second run (using the java interpreter).

Compilation Time vs Runtime

Compilation time --- The compiler (javac) knows, it knows at compile time.

Runtime --- Java Interpretor (java) knows, but it is at runtime, and Runtime polymorphism occurs between parent relationships of parent relationships. The child provides an override of the method declared in the parent class.

Compilation time compared to the example run-time polymorphism: -

 package com.deep.javazone.example2; public interface Tax { // Defining some default behaviour of tax calculation public double calculateTax(double amount); // Used to calculate the tax for particular no of years public double calculateTax(double amount, double years); // Used to calculate the tax for particular no of years excluding the the current year public double calculateTax(double amount, double years, Boolean excludeCurrentYear); } 

//// Implementation of sales tax

 package com.deep.javazone.example2; public class SalesTax implements Tax{ public double calculateTax(double amount){ double calculatedTax = 0.0; //Todo return calculatedTax; } public double calculateTax(double amount, double years){ double calculatedTax = 0.0; //Todo return calculatedTax; } public double calculateTax(double amount, double years, Boolean excludeCurrentYear){ double calculatedTax = 0.0; //Todo return calculatedTax; } } 

//// Taxation of services

 package com.deep.javazone.example2; public class ServiceTax implements Tax{ public double calculateTax(double amount){ double calculatedTax = 0.0; //Todo return calculatedTax; } public double calculateTax(double amount, double years){ double calculatedTax = 0.0; //Todo return calculatedTax; } public double calculateTax(double amount, double years, Boolean excludeCurrentYear){ double calculatedTax = 0.0; //Todo return calculatedTax; } } 

//// Tax Calculator

 package com.deep.javazone.example2; public class CalculateTax { public static void main(String[] args) { CalculateTax calculateTax = new CalculateTax(); // Sales Tax Tax tax = new SalesTax(); calculateTax.calculateTax(tax, 200000); calculateTax.calculateTax(tax, 2000000, 5); calculateTax.calculateTax(tax, 2000000, 5, false); //Service Tax tax = new ServiceTax(); calculateTax.calculateTax(tax, 200000); calculateTax.calculateTax(tax, 2000000, 5); calculateTax.calculateTax(tax, 2000000, 5, false); } public double calculateTax(Tax tax, double amount){ return tax.calculateTax(amount); } public double calculateTax(Tax tax, double amount, double noOfYEars){ return tax.calculateTax(amount, noOfYEars); } public double calculateTax(Tax tax, double amount, double noOfYEars, boolean currentYear){ return tax.calculateTax(amount, noOfYEars, currentYear); } } 

In the above example, when the main method of the CalculateTax classes is compiled, the compiler is very confident and understandable that overloaded methods of the CalculateTax class are called like all methods that have the same name but have different signatures (method parameters).

But when compiling another overloaded method code, the compiler has no idea that later at run time what type of tax link will be passed to the method parameters. For example, Tax is an interface, while ServiceTax and SalesTax are its implementation, which provides polymorphism at runtime.

Hope I was able to answer your question. :)

+2
source

Is the method an overload of the way to achieve compile-time polymorphism? If so, is this the only way? A small example helped me a lot.

First answer: yes.Because before starting the program, you know which method you will use by type and number of parameters, even if both methods have the same name.

 //the example of code to show compile time poly public class A{ public void fun(int a){ //do something } public void fun(String a){ //do something else } } 

Second answer: None. Sometimes the override method can be a compile-time polymorphism. Example:

 public class Father(){ public void say(){ System.out.println("Father"); } } public class Son extends Father{ public void say(){ System.out.println("Son"); } } 

Now compile-time polymorphism:

 public class Test{ public static void main(String args[]){ Father father=new Father(); father.say();//You know it is father,right? Son son=new Son(); son.say();//You know it is son,right? } } 

as a comparison:

 public class Test2{ public static void main(String args[]){ Father father=new Son();//the reference of Father point to the object of //Son father.say();//only runtime we know which say() is running here } } 
+1
source

Source: https://habr.com/ru/post/1266739/


All Articles