-
Notifications
You must be signed in to change notification settings - Fork 0
/
polimorfisms.java
114 lines (95 loc) · 2.25 KB
/
polimorfisms.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Manipulando polimorfismos
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}
class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myPig = new Pig();
Animal myDog = new Dog();
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
}
// Aninhando classes
class OuterClass {
int x = 10;
class InnerClass {
int y = 5;
}
}
class Print {
public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
}
}
// Classe interna estática
class inter {
int b = 10;
static class second {
int c = 5;
}
}
class third {
public static void main(String[] args) {
inter.second myInter = new inter.second();
System.out.println(myInter.c); /* Resultou apenas c,
por estar estática*/
}
}
abstract class animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}
class Gato extends animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("O gato faz: miau");
}
}
class fifth {
public static void main(String[] args) {
Gato meuGato = new Gato();
meuGato.animalSound();
meuGato.sleep();
}
}
// Manipulando interfaces
/* Interfaces são outro tipo de abstração, onde passamos uma interface
classe abstrata usada para agrupar métodos relacionados com corpos vazios*/
interface car {
public void carSound();
public void horn();
}
class aircross implements car {
public void carSound() {
System.out.println("Vruum");
}
public void horn() {
System.out.println("Biiii");
}
}
class action {
public static void main(String[] args) {
aircross myCar = new aircross();
myCar.carSound();
myCar.horn();
}
}