Instance abstract classes in Java

0

Every time I want to create an object of an abstract class with two daughters, I create the constructor by implementing the method.

package ultimaIntentona;

import java.util.ArrayList;

public abstract class Personal {

    static int  autonumerico;
    int numPersonal;
    String nombre;
    Cliente cliente;
    String registroPersonal;
    double sueldoBase;
    ArrayList<Proyecto> proyectosAsignados;

    public Personal() {//mis datos
        this.autonumerico++;
        this.numPersonal = autonumerico;
        this.nombre = "pepe";
        this.cliente = new Cliente();
        this.registroPersonal = "123a";
        this.sueldoBase = 3000.0;
        this.proyectosAsignados = new ArrayList<Proyecto>();
    }

    public abstract double calcularSueldo();

You have two daughter classes:

The first one,

package ultimaIntentona;

public class Programadores extends Personal{
    double sueldo;

    public Programadores(double sueldo) {
        this.sueldo = sueldo;
    }

    @Override
    public double calcularSueldo() {
        this.sueldo=getSueldoBase();
        return sueldo;
    }

And the other daughter

package ultimaIntentona;

public class Responsables extends Personal {
    double sueldo;
    int productividad;

public Responsables(int productividad) {
    super();
    this.productividad = productividad;
}

@Override
public double calcularSueldo() {
    this.sueldo=getSueldoBase()+productividad;
    return sueldo;
}

And every time I build an object forces me to implement the method. For example:

[

    
asked by David Palanco 24.05.2018 в 22:44
source

1 answer

1

When you enter an abstract class, it is as if you used an "incomplete" class since the implementation of the abstract methods is missing, Java allows you in that case to create an anonymous class to use it but forces you to define the pending abstract methods. Finally, if you do not want to implement these methods, you can use one of the child classes you have defined. For example:

Programadores p = new Programadores();

Responsables r = new Responsables();
  

An abstract class is a class that is declared abstract-it may or may   not include abstract methods. Abstract classes can not be instantiated,   but they can be subclassed.

     

An abstract method is a method that is declared without an   implementation (without braces, and followed by a semicolon)    link

In Spanish: an abstract class is a class that is declared with the keyword abtracts and may or may not contain abstract methods. An abstract class can not be instantiated but can inherit.

An abstract method is a method that is declared without implementation (without brackets and followed by a semicolon)

    
answered by 24.05.2018 в 23:56