Java is Everywhere

Study Java for a better life and future…

1. Polymorphism

(1.1) Compile-time Polymorphism

(1.1.1) Method Overloading: Multiple functions have the same name but with different parameters. Their parameters can contain different types and various numbers.

Example:

class summation{
// Method with 2 parameters
int sum (int a, int b){
return a+b;
}
// Method with the same name but 2 double parameters
double sum(double a, double b){
return a+b;
}
// Method with the same name but 3 parameters
int sum(int a, int b, int c){
return a+b+c;
}
}

(1.2) Runtime Polymorphism

(1.2.1) Method Overriding: In this process of Runtime Polymorphism, an overridden method is called by a reference variable of a superclass.

Example:

class BankAccount{
void getMoney(){
System.out.print('Get back the money from your account.');
return;
}
}
class TomBankAccount extends BankAccount{
void getMoney(){
System.out.print('Tom's Bank Account');
}
}
class TestPolymorphism{
public static void main(String[] args){
BankAccount a;
BankAccount b;
a = new BankAccount();
b = new TomBankAccount();
a.getMoney(); // runs the method in BankAccount class
b.getMoney(); // runs the method in TomBankAccount class
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store