heritage

Spread the love

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bestpractices;

/**
*
* @author VonTalavang
*/
public class Herencia {

public static void main(String[] args) {
Vehiculo v1 = new Vehiculo();
Vehiculo v2 = new Avion();
Vehiculo v3 = new Automovil();
pasarVehiculo(v1);
pasarVehiculo(v2);
pasarVehiculoAvion((Avion) v2);
pasarVehiculoAvion((Avion) v1); //this one will fail
}

private static void pasarVehiculo(Vehiculo v1) {
System.out.println(v1.getTipoVehiculo() + “, ” + v1.getClass().getSimpleName());
if(v1.getClass().getSimpleName().equalsIgnoreCase(“Avion”)){
Avion av = (Avion)v1;
System.out.println(av.despegar());
}
}

private static void pasarVehiculoAvion(Avion v1) {
System.out.println(v1.getTipoVehiculo() + “, ” + v1.getClass());
}

}

class Avion extends Vehiculo {

public Avion() {
super(“avion”);
}
public String despegar(){
return “nnnniuuum”;
}
}

class Automovil extends Vehiculo {

public Automovil() {
super(“automovil”);
}
}

class Vehiculo {

private String tipoVehiculo = “defecto”;

public Vehiculo() {
}

public Vehiculo(String tipoVehiculo) {
this.tipoVehiculo = tipoVehiculo;
}

public String getTipoVehiculo() {
return tipoVehiculo;
}

public void setTipoVehiculo(String tipoVehiculo) {
this.tipoVehiculo = tipoVehiculo;
}
}

Comments

comments

Leave a Reply