Home >Java >javaTutorial >What is the difference between compile-time polymorphism and run-time polymorphism?
Polymorphism is one of the most important OOP concepts. This is a concept where we can perform a single task in multiple ways. There are two types of polymorphism, one is compile-time polymorphism and the other is run-time polymorphism.
Method overloading is an example of compile-time polymorphism, and method overriding is an example of run-time polymorphism.
Method overloading is an example of compile-time polymorphism, and method overriding is an example of run-time polymorphism. p>
Advanced. | Key | Compile-time polymorphism | Run-time polymorphism |
---|---|---|---|
1 | Basic | Compile-time polymorphism means binding occurs at compile time | Run-time polymorphism property, at runtime we know which method will be called |
2 | static/dynamicBinding | ##can be achieved through static binding | can be achieved through dynamic binding Determined to achieve|
Inheritance | is not involved Inheritance |
| Involving inheritance|
Example | Method overriding is an example of compile-time polymorphism | Method overriding is an example of run-time polymorphism |
public class Main { public static void main(String args[]) { CompileTimePloymorphismExample obj = new CompileTimePloymorphismExample(); obj.display(); obj.display("Polymorphism"); } } class CompileTimePloymorphismExample { void display() { System.out.println("In Display without parameter"); } void display(String value) { System.out.println("In Display with parameter" + value); } }
public class Main { public static void main(String args[]) { RunTimePolymorphismParentClassExample obj = new RunTimePolymorphismSubClassExample(); obj.display(); } } class RunTimePolymorphismParentClassExample { public void display() { System.out.println("Overridden Method"); } } public class RunTimePolymorphismSubClassExample extends RunTimePolymorphismParentExample { public void display() { System.out.println("Overriding Method"); } }
The above is the detailed content of What is the difference between compile-time polymorphism and run-time polymorphism?. For more information, please follow other related articles on the PHP Chinese website!