-
Notifications
You must be signed in to change notification settings - Fork 343
Expand file tree
/
Copy pathPolymorphism.java
More file actions
63 lines (43 loc) · 1.25 KB
/
Polymorphism.java
File metadata and controls
63 lines (43 loc) · 1.25 KB
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
package basic.c08_oop;
/*
Clase 63 - Polimorfismo
Vídeo: https://youtu.be/JOAqpdM36wI?t=24505
*/
public class Polymorphism {
public static void main(String[] args) {
// Polimorfismo
// - Polimorfismo por herencia (sobrescritura)
var animal = new Animal();
animal.sound();
var dog = new Dog();
dog.sound();
// - Polimorfismo por sobrecarga (sobrecarga de métodos)
var calculator = new Calculator();
System.out.println(calculator.sum(3, 5));
System.out.println(calculator.sum(3.2, 5.4));
}
// - Polimorfismo por herencia (sobrescritura)
public static class Animal {
public void sound() {
System.out.println("Algún sonido");
}
}
public static class Dog extends Animal {
@Override
public void sound() {
System.out.println("Guau");
}
}
// - Polimorfismo por sobrecarga (sobrecarga de métodos)
public static class Calculator {
public int sum(int a, int b) {
return a + b;
}
public int sum(int a, int b, int c) {
return a + b + c;
}
public double sum(double a, double b) {
return a + b;
}
}
}