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 {
//// Implementation of sales tax
package com.deep.javazone.example2; public class SalesTax implements Tax{ public double calculateTax(double amount){ double calculatedTax = 0.0;
//// Taxation of services
package com.deep.javazone.example2; public class ServiceTax implements Tax{ public double calculateTax(double amount){ double calculatedTax = 0.0;
//// Tax Calculator
package com.deep.javazone.example2; public class CalculateTax { public static void main(String[] args) { CalculateTax calculateTax = new CalculateTax();
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. :)