-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolymorphsim.txt
32 lines (31 loc) · 1.15 KB
/
polymorphsim.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
polymorphism:
Polymorphism in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs.
The word "poly" means many and "morphs" means forms. So polymorphism means many forms.
There are the two types of polymorphsim.
1) runtime
Runtime polymorphism or Dynamic Method Dispatch is a
process in which a call to an overridden method is resolved at runtime rather than compile-time.
Examples
class Bike{
void run(){System.out.println("running");}
}
class Splendor extends Bike{
void run(){System.out.println("running safely with 60km");}
public static void main(String args[]){
Bike b = new Splendor();//upcasting
b.run();
}
}
2)coompile time
Compile-time polymorphism, sometimes referred to as static polymorphism or early binding, is the capacity of a programming language
to decide which method or function to use based on the quantity,
Example:
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}