Hola usuario de nuestra página web, encontramos la solución a lo que andabas buscando, continúa leyendo y la verás más abajo.
Ejemplo 1: herencia en java
// super classclassAnimalvoideat()System.out.println("Animal is eating.");// subclassclassLionextendsAnimalpublicstaticvoidmain(String[] args)Lion obj =newLion();
obj.eat();
Ejemplo 2: herencia en java
// Multilevel inheritance in javaclassAnimalvoideating()System.out.println("animal eating");classLionextendsAnimalvoidroar()System.out.println("lion roaring");publicclassCubextendsLionvoidborn()System.out.println("cub drinking milk");publicstaticvoidmain(String[] args)Animal obj1 =newAnimal();
obj1.eating();Lion obj2 =newLion();
obj2.eating();
obj2.roar();Cub obj3 =newCub();
obj3.eating();
obj3.roar();
obj3.born();
Ejemplo 3: herencia en java
// Hierarchical inheritance in javaclassAnimalvoideating()System.out.println("animal eating");classLionextendsAnimalvoidroar()System.out.println("lion roaring");publicclassDogextendsAnimalvoidbark()System.out.println("dog barking");publicstaticvoidmain(String[] args)Animal obj1 =newAnimal();
obj1.eating();Lion obj2 =newLion();
obj2.eating();
obj2.roar();Dog obj3 =newDog();
obj3.eating();
obj3.bark();
Ejemplo 4: herencia en java
// Hybrid inheritance in javaclassApublicvoiddisplay()System.out.println("class A display method");interfaceBpublicvoidprint();interfaceCpublicvoidprint();publicclassHybridInheritanceDemoextendsAimplementsB,Cpublicvoidprint()System.out.println("implementing print method");publicvoidshow()System.out.println("show method of class HybridInheritanceDemo");publicstaticvoidmain(String[] args)HybridInheritanceDemo obj =newHybridInheritanceDemo();
obj.show();
obj.print();
Ejemplo 5: herencia en java
// single inheritance exampleimportjava.util.*;classAnimalvoideat()System.out.println("Animal is eating.");classLionextendsAnimalvoidroar()System.out.println("lion is roaring.");publicstaticvoidmain(String[] args)Lion obj =newLion();
obj.eat();
obj.roar();
Ejemplo 6: Herencia en Java
JavaInheritance(Subclass and Superclass)InJava, it is possible toinherit attributes and methods from one classtoanother.We group the "inheritance concept" into two categories:
subclass (child)- the class that inherits from another class
superclass (parent)- the class being inherited from
To inherit from a class, use the extends keyword.
Si te animas, tienes la libertad de dejar un tutorial acerca de qué te ha gustado de este artículo.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)