



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




¿Qué es la herencia en Java?
Ejemplo práctico: Clase Padre class Animal { String nombre; int edad; public Animal(String nombre, int edad) { this.nombre = nombre; this.edad = edad; } public void hacerSonido() { System.out.println("El animal hace un sonido."); } }
Ejemplo práctico: Clase Hija class Perro extends Animal { public Perro(String nombre, int edad) { super(nombre, edad); } public void moverCola() { System.out.println(nombre + " mueve la cola."); } @Override public void hacerSonido() { System.out.println(nombre + " dice: ¡Guau guau!"); } }
Ventajas de la herencia