forked from mouredev/hello-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritanceExercises.java
More file actions
96 lines (75 loc) · 3.7 KB
/
Copy pathInheritanceExercises.java
File metadata and controls
96 lines (75 loc) · 3.7 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
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
package basic.c08_oop;
/*
Clase 62 - Ejercicios: Herencia
Vídeo: https://youtu.be/JOAqpdM36wI?t=24373
*/
import java.util.ArrayList;
public class InheritanceExercises {
public static void main(String[] args) {
// 1. Crea una clase Vehicle con un método move(). Luego crea una subclase Car que herede de Vehicle y agrega el método honk().
Car myCar = new Car("Kia", "Cerato");
// Método heredado de Vehicle
myCar.move();
// Métodos propios de Car
myCar.accelerate(50);
myCar.brake(20);
myCar.honk();
myCar.showData();
// 2. Define una clase Person con los atributos name y age. Luego crea una clase Student que agregue el atributo grade y un método study().
Student s1 = new Student("Ana", 20, "S001", 75);
s1.sayHello(); // heredado de Person
s1.study(); // propio de Student
s1.result(); // imprime si aprobó
// 3. Crea una clase Animal con el método makeSound(). Haz que Dog diga “Woof” y Cat diga “Meow” sobrescribiendo ese método.
Dog dog = new Dog();
dog.bark(); // Woof
Cat cat = new Cat();
cat.makeSound(); // Meow
// 4. La clase Employee tiene los atributos name y salary. Manager hereda de Employee y agrega el atributo department.
Manager m = new Manager(3000, "Ventas");
m.showInfo(); // Departamento: Ventas, Salario: 3000.0
m.raiseSalary(10); // Salario aumentado en 10%, nuevo salario: 3300.0
// 5. Crea una clase abstracta Shape con un método calculateArea(). Luego implementa ese método en Circle y Rectangle.
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4, 3);
System.out.println("Área del círculo: " + circle.calculateArea());
System.out.println("Área del rectángulo: " + rectangle.calculateArea());
// 6. Crea una clase Bird con el método fly(). Luego crea Eagle que sobrescriba fly() pero también llame al método original con super.fly().
Bird genericBird = new Bird();
genericBird.fly();
System.out.println("------");
Eagle eagle = new Eagle();
eagle.fly();
// 7. Haz una clase Device con un constructor que imprima “Device created”. Luego crea Phone que herede de Device y en su constructor imprima “Phone ready”.
Phone myPhone = new Phone();
// 8. Account tiene un saldo y métodos para deposit() y withdraw(). SavingsAccount hereda y agrega un método addInterest().
SavingsAccount myAccount = new SavingsAccount(1000, 5); // saldo 1000, interés 5%
myAccount.deposit(200); // Deposita 200
myAccount.withdraw(100); // Retira 100
myAccount.addInterest(); // Agrega intereses
// 9. Crea una clase Vehicle y tres subclases: Car, Bike y Truck, cada una con un método describe() sobrescrito.
Vehicle[] vehicles = {
new Car("Kia", "Cerato"),
new Bike("Montaña"),
new Truck(10)
};
for (Vehicle v : vehicles) {
v.move();
v.describe();
if (v instanceof Car car) { // usar funcionalidades de Car
car.accelerate(50);
car.brake(20);
car.honk();
}
System.out.println();
}
// 10. Crea un ArrayList<Animal> que contenga instancias de Dog, Cat y Bird. Recorre la lista y llama a makeSound().
ArrayList<Animal> animals = new ArrayList<>();
animals.add(new Dog());
animals.add(new Cat());
animals.add(new Bird());
for (Animal animal : animals) {
animal.makeSound();
}
}
}