-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolymorphismTest.java
64 lines (51 loc) · 1.45 KB
/
PolymorphismTest.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Method Overloading
class Operation{
static int Add(int ...args){
int sum = 0;
for(int arg : args)
sum += arg;
return sum;
}
static double Add(double ...args){
double sum = 0;
for(double arg : args)
sum += arg;
return sum;
}
}
// Method Overriding
class Parant{
void print(){
System.out.println("I'm Parant");
}
}
class Child1 extends Parant{
@Override void print(){
System.out.println("I'm Child1");
}
}
class Child2 extends Parant{
@Override void print(){
System.out.println("I'm Child2");
}
}
public class PolymorphismTest{
public static void main(String args[]){
System.out.println("------------------------------------------------");
System.out.println("Polymorphism testing . . . \n");
// Method Overloading
System.out.println("1.) Method Overloading Example . . . ");
System.out.println("Adding 1,2,3,4,5 : " + Operation.Add(1,2,3,4,5));
System.out.println("Adding 1.234,2.14,3,4.7,5.98 : " + Operation.Add(1.234,2.14,3,4.7,5.98));
// Method Overriding
System.out.println("\n2.) Method Overriding Example . . . ");
Parant a;
a = new Parant();
a.print();
a = new Child1();
a.print();
a = new Child2();
a.print();
System.out.println("------------------------------------------------");
}
}