daughter class method visible from the superclass

0

I have a superclass (Footwear) and daughter classes (Shoe-Shoe). I have a getIva () method in the Slipper class and I need to recover this Iva value from the superclass.

The handling with the super is resolved, and I do not identify how to recover the data.

import java.util.*;

public class Ejecutora {
    public static void main (String args[]) {
    Principal p = new Principal();
    Scanner e = new Scanner(System.in);
    int opc = 0;
    do {
        System.out.println("1 - AGREGA UN CALZADO" );
        System.out.println("2 - LISTA ARTICULOS ");
        System.out.println("3 - LISTA PRECIOS CON IVA");
        System.out.println("4 - LISTAR COLORES");
        opc = e.nextInt();
        switch ( opc ) {
        case 1: {
            p.agregaCalzado();
            break;
        }
        case 2: {
            p.listaArticulos();
            break;
        }
        case 3: {
            p.listarPrecio();
            break;
        }
        case 4: {
            p.listarColores();
            break;
        }
        }
    } while ( opc != 0);
    }
    } 

import java.util.*;
public class Principal {
private ArrayList<Calzado>calzados;

public Principal(){
    calzados = new ArrayList<Calzado>();
}

public void agregaCalzado() {
    Scanner e = new Scanner(System.in);
    e.useDelimiter(System.getProperty("line.separator"));
    System.out.println("INGRESE EL CODIGO DEL CALZADO");
    int codi = e.nextInt();
    Calzado c = this.buscaCalzado(codi);
    if ( c != null ) {
        System.out.println("EL CALZADO YA FUE CARGADO");
    }
    else {
        int opc = 0;
        do {
            System.out.println("1- ZAPATILLA  | 2 - ZAPATO ");
            opc = e.nextInt();
        } while ( opc != 1 && opc != 2);

        System.out.println("INGRESE EL DETALLE DEL CALZADO");
        String detalle = e.next();
        System.out.println("INGRESE EL TALLE DEL CALZADO");
        float tall = e.nextFloat() ;
        System.out.println("INGRESE EL PRECIO DEL CALZADO");
        float prec = e.nextFloat();
        Calzado nuevo;
        if ( opc == 1 ) {
            System.out.println("INGRESE EL IVA");
            float iva = e.nextFloat();
            nuevo = new Zapatilla ( codi, detalle, tall, prec, iva );
        }
        else {
            System.out.println("INGRESE LA COMISION");
            float comi = e.nextFloat();
            nuevo = new Zapato ( codi, detalle, tall, prec, comi );
        }
        calzados.add(nuevo);
    }
}

public void listarColores() {
    for ( Calzado ca: calzados) {
        System.out.println("EL DETALLE ES: " + ca.getDetalle());
    }
}

public void listaValores() {
    for ( Calzado c: calzados ) {
        System.out.println("DETALLE: " + c.getDetalle() + "PRECIO: " + c.getPrecio() + "IVA: " );
    }
}

public void listarPrecio() {
    float sumaPrecio = 0;
    for (Calzado ca: calzados) {
        sumaPrecio = sumaPrecio + ca.darPrecio();
        System.out.println("LOS PRECIO SON: " + ca.darPrecio() + "CODIGO: " + ca.getCodigo());
    }
    System.out.println("LA SUMA ES DE PRECIO ES: " + sumaPrecio);
}

public void listaArticulos() {
    for ( Calzado c: calzados ) {
        System.out.println("codigo: " + c.getCodigo() + " detalle: " + c.getDetalle());
    }
}

public Calzado buscaCalzado(int c) {
    int a = 0;
    while ( a < calzados.size() && !( calzados.get(a).sosCodigo(c)))
        a ++;
    if ( a < calzados.size())
        return calzados.get(a);
    else 
        return null;
}
}


public abstract class Calzado {
private int codigo;
private String detalle;
private float talle;
private float precio;

public Calzado ( int cod, String det, float tal, float pre ) {
    codigo = cod;
    detalle = det;
    talle = tal;
    precio = pre;
}


public boolean sosCodigo(int a) {
    return (codigo == a);
}

public float darPrecio() {
    return precio;
}

public int getCodigo() {
    return codigo;
}

public String getDetalle() {
    return detalle;
}

public float getTalle() {
    return talle;
}

public float getPrecio() {
    return precio;
}
}

public class Zapatilla extends Calzado {
private float iva;

public Zapatilla ( int cod, String col, float tal, float pre, float i ) {
    super ( cod, col, tal, pre );
    iva = i;
}

public float darPrecio() {
    float res = 0;
    res = ( iva * 5 + super.darPrecio());
    return res;
}

public float getIva() {
    return iva;
}
}


public class Zapato extends Calzado {
private float comision;

public Zapato ( int cod, String col, float tal, float pre, float comis ) {
    super (cod, col, tal, pre);
    comision = comis;
}

public float darComision() {
    return comision;
}

public float darPrecio() {
    float cos = 0;
    cos = ( comision * 10 + super.darPrecio());
    return cos;
}
}
    
asked by Mauricio Vega 02.07.2018 в 05:24
source

1 answer

1

There are 2 ways you can do it.

Declaring the method in the base class and so you can access the value even if the declaration is of the father type:

public abstract class Padre {
 public abstract int getIva();
}

public class Hija extends Padre {
  @Override
  public int getIva(){ return 44; }
}

Padre padre = new Hija();
System.out.println(padre.getIva().toString()); // 44

Or you create an interface that contains the getIva() method and implement it in the classes that require that method. Then you will have to ask if the instance is of the type of the interface and then cast and get the value:

public interface IIvaInterface{
    int getIva();
}
public abstract class Padre {
    //...
}

public class Hija extends Padre implements IIvaInterface {
    @Override
    public int getIva(){ return 44; }
}

Padre padre = new Hija();
int iva = 0;
if(padre instanceof IIvaInterface)
{
  iva = ((IIvaInterface)padre).getIva();
}

System.out.println(iva.toString()); // 44
    
answered by 02.07.2018 / 15:38
source