Polymorphism
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Overloading
Methods with the same names in a class are allowed provided each method has a different signature. Overloading can be done in the following ways:
- Number of arguments
- Type of arguments
- Position of arguments
An example of a Java program with overloading
The following is an example of overloading in a Java program:
package MyFirstPackage;
class SampleClass10 {
void sampleMethod(){
System.out.println("executing sample method");
}
void sampleMethod(float b){
System.out.println("executing sample method" + b);
}
}
class SampleClass20 extends SampleClass10 {
void sampleMethod1(){
System.out.println("executing sample method 2");
}
void sampleMethod1(int a){
System.out.println("executing sample method 2" + a);
}
}
public...