Skip to content

Commit a63ceb3

Browse files
committed
Clase 6 | 14/05/2025
Clases, encapsulamiento y herencia
1 parent bf588b9 commit a63ceb3

7 files changed

Lines changed: 244 additions & 3 deletions

File tree

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
1212
## 🆕 NUEVO: Curso desde cero [En desarrollo]
1313

14-
### 🔴 PRÓXIMA CLASE EN DIRECTO: Miércoles 14 de mayo a las 20:00h (España) en [Twitch](https://twitch.tv/mouredev)
15-
### 🗓️ CONSULTA EL HORARIO POR PAÍS Y CREA UN RECORDATORIO desde [Discord](https://discord.com/events/729672926432985098/1367238643906773103)
14+
### 🔴 PRÓXIMA CLASE EN DIRECTO: Miércoles 21 de mayo a las 20:00h (España) en [Twitch](https://twitch.tv/mouredev)
15+
### 🗓️ CONSULTA EL HORARIO POR PAÍS Y CREA UN RECORDATORIO desde [Discord](https://discord.com/events/729672926432985098/1372312583033585824/1374808965120000000)
1616

1717
### Clases:
1818

@@ -39,11 +39,18 @@
3939
* [Mapas](./basic/c05_structures/Maps.java)
4040
* [Ejercicios](./basic/c05_structures/StructuresExercises.java)
4141

42-
* Clase 5 [17/04/2025] - Bucles y funciones
42+
* Clase 5 [06/05/2025] - Bucles y funciones
4343
* [Vídeo](https://www.twitch.tv/videos/2452053839)
4444
* [Bucles](./basic/c06_loops/Loops.java) | [Ejercicios](./basic/c06_loops/LoopsExercises.java)
4545
* [Funciones](./basic/c07_functions/Functions.java) | [Ejercicios](./basic/c07_functions/FunctionsExercises.java)
4646

47+
* Clase 6 [14/05/2025] - Clases, encapsulamiento y herencia
48+
* [Vídeo](https://www.twitch.tv/videos/2459212698)
49+
* [Clases](./basic/c08_oop/Classes.java) | [Clase Persona](./basic/c08_oop/Person.java) | [Ejercicios](./basic/c08_oop/ClassesExercises.java)
50+
* [Encapsulamiento](./basic/c08_oop/AccessModifiers.java) | [Clases (otro paquete para pruebas)](./basic/c08_test_oop/Classes.java)
51+
* [Herencia](./basic/c08_oop/Inheritance.java)
52+
53+
4754
## Enlaces de interés
4855

4956
* Impacto: [Índice TIOBE](https://www.tiobe.com/tiobe-index/) | [GitHub](https://github.blog/news-insights/octoverse/octoverse-2024/) | [Stack Overflow](https://survey.stackoverflow.co/2024/technology#most-popular-technologies-language)

basic/c08_oop/AccessModifiers.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package basic.c08_oop;
2+
3+
/*
4+
Clase 6 - Clases, encapsulamiento y herencia (14/05/2025)
5+
Vídeo: https://www.twitch.tv/videos/2459212698
6+
*/
7+
8+
public class AccessModifiers {
9+
10+
public static void main(String[] args) {
11+
12+
// Encapsulamiento
13+
14+
// - Modificadores de acceso
15+
// public
16+
// private
17+
// protected
18+
// (default)
19+
20+
// getters
21+
// setters
22+
23+
}
24+
}

basic/c08_oop/Classes.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package basic.c08_oop;
2+
3+
/*
4+
Clase 6 - Clases, encapsulamiento y herencia (14/05/2025)
5+
Vídeo: https://www.twitch.tv/videos/2459212698
6+
*/
7+
8+
public class Classes {
9+
10+
public static void main(String[] args) {
11+
12+
var person = new Person("Brais", -38, "123456789A");
13+
14+
// person.name = "Brais";
15+
// person.age = 38;
16+
17+
person.sayHello();
18+
19+
person.name = "Brais Moure";
20+
System.out.println(person.name);
21+
22+
// person.id = "123456789A";
23+
24+
System.out.println(person.getId());
25+
26+
person.setAge(38);
27+
System.out.println(person.getAge());
28+
29+
var person2 = new Person("MoureDev", 18, "123456789B");
30+
person2.sayHello();
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package basic.c08_oop;
2+
3+
/*
4+
Clase 6 - Clases, encapsulamiento y herencia (14/05/2025)
5+
Vídeo: https://www.twitch.tv/videos/2459212698
6+
*/
7+
8+
public class ClassesExercises {
9+
10+
public static void main(String[] args) {
11+
12+
// 1. Crea una clase Book con atributos title y author. Crea un objeto y muestra sus datos.
13+
14+
// 2. Crea una clase Dog con un método bark() que imprima su sonido.
15+
16+
// 3. Añade un constructor a la clase Book que reciba title y author.
17+
18+
// 4. Crea una clase Car con atributos brand y model y un método showData().
19+
20+
// 5. Crea una clase Student con atributo score y un método que diga si aprobó (mayor o igual a 60).
21+
22+
// 6. Crea una clase BankAccount con atributo balance y un método deposit() que sume el saldo.
23+
24+
// 7. Crea una clase Rectangle con métodos para calcular el área y el perímetro.
25+
26+
// 8. Crea una clase Worker que reciba nombre y salario, y un método para mostrar su salario.
27+
28+
// 9. Crea varios objetos Person y guárdalos en un ArrayList.
29+
30+
// 10. Crea una clase Product y un método que aplique un descuento sobre su precio.
31+
}
32+
}

basic/c08_oop/Inheritance.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package basic.c08_oop;
2+
3+
/*
4+
Clase 6 - Clases, encapsulamiento y herencia (14/05/2025)
5+
Vídeo: https://www.twitch.tv/videos/2459212698
6+
*/
7+
8+
public class Inheritance {
9+
10+
public static void main(String[] args) {
11+
12+
// Herencia
13+
14+
var animal = new Animal("Mi animal");
15+
// animal.name = "Mi animal";
16+
animal.eat();
17+
18+
var dog = new Dog("Mou", 3);
19+
// dog.name = "Mou";
20+
dog.eat();
21+
22+
var cat = new Cat("Cou");
23+
// cat.name = "Cou";
24+
cat.eat();
25+
26+
var bird = new Bird("Bou");
27+
// bird.name = "Bou";
28+
bird.eat();
29+
bird.fly();
30+
}
31+
32+
public static class Animal {
33+
34+
String name;
35+
36+
public Animal(String name) {
37+
this.name = name;
38+
}
39+
40+
public void eat() {
41+
System.out.println("El animal con nombre " + name + " está comiendo.");
42+
}
43+
}
44+
45+
public static class Dog extends Animal {
46+
47+
int age;
48+
49+
public Dog(String name, int age) {
50+
super(name);
51+
this.age = age;
52+
}
53+
54+
@Override
55+
public void eat() {
56+
System.out.println("El perro con nombre " + name + " está comiendo.");
57+
}
58+
}
59+
60+
public static class Cat extends Animal {
61+
62+
public Cat(String name) {
63+
super(name);
64+
}
65+
}
66+
67+
public static class Bird extends Animal {
68+
69+
public Bird(String name) {
70+
super(name);
71+
}
72+
73+
public void fly() {
74+
System.out.println("Está volando");
75+
}
76+
}
77+
78+
}

basic/c08_oop/Person.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package basic.c08_oop;
2+
3+
/*
4+
Clase 6 - Clases, encapsulamiento y herencia (14/05/2025)
5+
Vídeo: https://www.twitch.tv/videos/2459212698
6+
*/
7+
8+
public class Person {
9+
10+
// Atributos
11+
protected String name;
12+
private int age;
13+
final private String id;
14+
15+
// Constructor
16+
public Person(String name, int age, String id) {
17+
this.name = name;
18+
this.setAge(age);
19+
this.id = id;
20+
}
21+
22+
// Métodos
23+
public void sayHello() {
24+
System.out.println("Hola, soy " + name + ", tengo " + age + " años, y mi id es " + id + ".");
25+
}
26+
27+
// Getter
28+
public int getAge() {
29+
return age;
30+
}
31+
32+
public String getId() {
33+
return id;
34+
}
35+
36+
// Setter
37+
public void setAge(int age) {
38+
if (age > 0) {
39+
this.age = age;
40+
} else {
41+
System.out.println("Edad no válida");
42+
}
43+
}
44+
}

basic/c08_test_oop/Classes.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package basic.c08_test_oop;
2+
3+
/*
4+
Clase 6 - Clases, encapsulamiento y herencia (14/05/2025)
5+
Vídeo: https://www.twitch.tv/videos/2459212698
6+
*/
7+
8+
import basic.c08_oop.Person;
9+
10+
public class Classes {
11+
12+
public static void main(String[] args) {
13+
14+
var person = new Person("Brais", 38, "123456789A");
15+
16+
// person.name = "Brais";
17+
// person.age = 38;
18+
19+
person.sayHello();
20+
21+
// person.name = "Brais Moure";
22+
// System.out.println(person.name);
23+
}
24+
}

0 commit comments

Comments
 (0)