Posts

Showing posts with the label compile time polymorphism

Polymorphism in Java: Definition, Types, and Examples

Polymorphism in Java Polymorphism is an object-oriented programming concept in which a single reference variable can exhibit different behaviors depending on the object it refers to. Types of Polymorphism Polymorphism in Java is classified into two types: Compile-Time Polymorphism Run-Time Polymorphism 1. Compile-Time Polymorphism In compile-time polymorphism, the method resolution is performed by the compiler. Key Points The compiler resolves the method call based on the parameter list. Once method resolution is done, the compiler performs static binding. In static binding:               ✓ The method declaration is bound to the method definition at compile time. Compile-time polymorphism is achieved using method overloading. Example class Calculator {     int add(int a, int b) {         return a + b;     }     double add(double a, double b) {         return a + b;     } } ...

Method Overloading in Java: Definition, Rules, and Examples

Method Overloading Method Overloading is the process of creating multiple methods with the same method name but different parameter lists within a class or across classes using inheritance. Key Points Method overloading can be achieved within the same class and also in different classes using inheritance. Method overloading can be done with: Static methods Instance methods Static final methods Instance final methods In method overloading, the compiler gives priority to the method name and parameter list. Return type is not considered for method overloading. Method overloading is also known as compile-time polymorphism. Ways to Achieve Method Overloading Method overloading can be achieved in three ways: By increasing the number of parameters void add(int a)   void add(int a, int b) By changing the data type of parameters void add(int a)   void add(double a) By changing the order of parameters void add(int a, double b)   void add(double a, int b) Important No...