Skip to content

Commit bc82f36

Browse files
committed
Herencia de Animal,perro y gato
1 parent b422e15 commit bc82f36

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package Herencia;
2+
/*
3+
Crear una superclase "Animal" con atributos como especie, nombre y edad.
4+
También debe tener métodos para moverse y hacer sonidos.
5+
Crear dos subclases de "Animal": "Perro" y "Gato".
6+
Ambas deben tener atributos adicionales como raza o si es un gato doméstico o salvaje.
7+
*/
8+
public class Animal {
9+
protected String especie;
10+
protected String nombre;
11+
protected int edad;
12+
//constructor
13+
public Animal(String especie, String nombre, int edad) {
14+
this.especie = especie;
15+
this.nombre = nombre;
16+
this.edad = edad;
17+
}
18+
//geters and setters
19+
public String getEspecie() {
20+
return especie;
21+
}
22+
public void setEspecie(String especie) {
23+
this.especie = especie;
24+
}
25+
public String getNombre() {
26+
return nombre;
27+
}
28+
public void setNombre(String nombre) {
29+
this.nombre = nombre;
30+
}
31+
public int getEdad() {
32+
return edad;
33+
}
34+
public void setEdad(int edad) {
35+
this.edad = edad;
36+
}
37+
//metodo moverse;
38+
public void moverse() {
39+
System.out.println(this.nombre+" esta moviendose");
40+
}
41+
//metodo hacer sonidos;
42+
public void hacerSonido() {
43+
System.out.println(this.nombre + " está haciendo un sonido.");
44+
}
45+
//metodo toString()
46+
@Override
47+
public String toString() {
48+
StringBuilder builder = new StringBuilder();
49+
builder.append("Animal [especie=");
50+
builder.append(this.especie);
51+
builder.append(", nombre=");
52+
builder.append(this.nombre);
53+
builder.append(", edad=");
54+
builder.append(this.edad);
55+
builder.append("]");
56+
return builder.toString();
57+
}
58+
59+
}

Ejercicios/src/Herencia/Gato.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Herencia;
2+
3+
public class Gato extends Animal{
4+
private boolean esDomestico;
5+
6+
public Gato(String especie, String nombre, int edad, boolean esDomestico) {
7+
super(especie, nombre, edad);
8+
this.esDomestico = esDomestico;
9+
}
10+
//getter and setter
11+
public boolean isDomestico() {
12+
return esDomestico;
13+
}
14+
15+
public void setEsDomestico(boolean esDomestico) {
16+
this.esDomestico = esDomestico;
17+
}
18+
public void maullar() {
19+
System.out.println(nombre + " está maullando.");
20+
}
21+
@Override
22+
public String toString() {
23+
StringBuilder builder = new StringBuilder();
24+
builder.append("{");
25+
builder.append(super.toString());
26+
builder.append(",");
27+
builder.append("Gato [esDomestico=");
28+
builder.append(this.esDomestico);
29+
builder.append("]");
30+
return builder.toString();
31+
}
32+
33+
}

Ejercicios/src/Herencia/Perro.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Herencia;
2+
3+
public class Perro extends Animal{
4+
private String raza;
5+
//constructor
6+
public Perro(String especie, String nombre, int edad, String raza) {
7+
super(especie, nombre, edad);
8+
this.raza = raza;
9+
}
10+
//getter and setter
11+
public String getRaza() {
12+
return raza;
13+
}
14+
public void setRaza(String raza) {
15+
this.raza = raza;
16+
}
17+
public void ladrar() {
18+
System.out.println(nombre + " está ladrando.");
19+
}
20+
//metodo toString()
21+
@Override
22+
public String toString() {
23+
StringBuilder builder = new StringBuilder();
24+
builder.append("{");
25+
builder.append(super.toString());
26+
builder.append(",");
27+
builder.append("Perro [raza=");
28+
builder.append(this.raza);
29+
builder.append("]");
30+
return builder.toString();
31+
}
32+
33+
}

0 commit comments

Comments
 (0)