Compile time Polymorphism
This program demonstrates compile time polymorphism.
Problem Statement
Write a Java program to show compile time polymorphism using method overloading.
Source Code
| 1 | class MathUtil { |
| 2 | int add(int a,int b){ return a+b; } |
| 3 | double add(double a,double b){ return a+b; } |
| 4 | } |
| 5 | public class CompileTimePolymorphism { |
| 6 | public static void main(String[] args) { |
| 7 | System.out.println("Eduinq Compile Time Polymorphism"); |
| 8 | MathUtil m=new MathUtil(); |
| 9 | System.out.println("Sum int: "+m.add(5,10)); |
| 10 | System.out.println("Sum double: "+m.add(5.5,10.5)); |
| 11 | } |
| 12 | } |
Program Output
Eduinq Compile Time Polymorphism Sum int: 15 Sum double: 16.0
Explanation
Method overloading resolved at compile time. - Compiler decides which method to call based on argument types. - This is compile time polymorphism, also called static binding. Conclusion: Shows compile time polymorphism using overloading.