-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolymorphism.java
More file actions
54 lines (40 loc) · 1012 Bytes
/
Polymorphism.java
File metadata and controls
54 lines (40 loc) · 1012 Bytes
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
// Compile-time Polymorphoisim
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
//Run-time Polymorphism
class Animal {
void sound() {
System.out.println("Animal is making a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog is barking");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat is meowing");
}
}
public class Polymorphism {
public static void main(String[] args) {
//Compile-time Polymorphism
Calculator calc = new Calculator();
System.out.println("Sum: " + calc.add(5, 10));
System.out.println("Sum: " + calc.add(5.5, 10.5));
// Run-time Polymorphism
Animal animal = new Dog();
animal.sound();
Animal newanimal = new Cat();
newanimal.sound();
Animal animal1 = new Animal();
animal1.sound();
}
}