Use of super keyword in abstract classes

1

Someone could tell me that exactly the reserved word super is used in this exercise (it is used to call the constructor of the parent class) but I do not understand why the argument String nom in the parent class is defined as this class abstract and can not be instantiated ... I'm starting in Java following the course of informational pills in case they wonder what I do and I do not know how to xD, Thanks in advance!

package poo;

import java.util.Date;
import java.util.GregorianCalendar;

public class Uso_Persona {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Persona[] laspersonas=new Persona[2];

        laspersonas[0]=new Empleado2("Luis Conde",50000,2009,02,25);
        laspersonas[1]=new Alumno("Ana Lopez","Biologicas");

        for(Persona p: laspersonas){
            System.out.println(p.damenombre()+", "+p.damedescripcion());
        }
    }
}

abstract class Persona{

    private String nombre;
    public abstract String damedescripcion();

    public Persona(String nom) {
        nombre=nom;
    }

    public String damenombre() {
        return nombre;
    }
}

class Empleado2  extends Persona{

    public Empleado2(String nom,double sue,int agno,int mes,int dia) {

        super(nom);

        sueldo=sue;

        GregorianCalendar calendario=new GregorianCalendar(agno,mes -1,dia);

        altaContrato=calendario.getTime();

        id=idsiguiente;

        idsiguiente++;

    }

    public String damedescripcion() {
        return"Este empleado tiene un id="+id+" con un sueldo="+ sueldo;
    }

    public double damesueldo() { //Getter
        return sueldo;
    }

    public Date damefechacontrato() { //Getter
        return altaContrato;
    }

    public int dameid(){
        return id;
    }

    public void subesueldo(double porcentaje) { //setter

        double aumento=sueldo*porcentaje/100;

        sueldo+=aumento;
    }

    private double sueldo;

    private Date altaContrato;

    private int id;

    private static int idsiguiente=1;
}

class Alumno extends Persona{
    private String carrera;

    public Alumno(String nom,String car) {

        super(nom);

        carrera=car;
    }

    public String damedescripcion(){
        return"Este alumno esta estudiando la carrera de="+carrera;
    }
}
    
asked by Jony 21.04.2018 в 03:18
source

1 answer

0

Abstract classes, although you can not create concrete ones, serve to define common characteristics and behaviors in a hierarchy of classes where each of the classes that inherit from this abstract, are nothing but a more specific type. Generally a class is defined as abstract or because your program does not need to handle objects of this type and / or because this class does not have all the necessary information to perform any task (hence the class Persona have the method abstract damedescripcion() since from that class you do not have how to know the correct description that you must return).

In the hierarchy of your example, you have a Persona class from which the Empleado2 and Alumno inherit, which, by inheriting from Persona share the attribute name, since both an employee and a student have a name. It is important to mention that this is a very simple case, but Persona could have many more attributes. If the class Persona had not been created, both Empleado2 and Alumno would have to add the corresponding code to handle the name. Then, as the name is (correctly) defined as an attribute of the class Persona , as the constructors of the classes are not inherited and as the person constructor defines that the value that will be assigned to said attribute when the create the instance of the class, is that it is necessary to call the constructor of the parent class in the constructors of the child classes and pass it the name.

Inheritance in an incredible mechanism for the reuse of code, since all the code defined in a parent class is as if you were defining it equally in the daughters. Maybe in a small hierarchy it is not noticeable, but in a large one, and that requires being able to be scaled, it shows a lot.

I know it's complicated to understand from the beginning and more just with an answer here, but I hope it helps you.

    
answered by 21.04.2018 / 04:04
source