I am making a program to practice the subject of inheritance and polymorphism. This consists of four main classes in the first instance to the Employee class (base for the others) followed by Secretary, Salesperson and Area Manager. Each of them with its attributes and methods, inheriting mostly the data from the main class (Employee). The problem is that I don't know how to initialize the constructor when the parameter asks me to put an object.
base class
package taller.relaciones.de.clases.parte.pkg2;
/**
*
* @author Usuario
*/
public class Empleado {
// atributos
String nombre;
String apellidos;
int DNI;
String direccion;
int años_antiguedad;
int telefono;
double salario;
Empleado supervisor;
// metodos
Empleado(String nombre, String apellidos, int DNI, String direccion, int años_antiguedad, int telefono, double salario, Empleado supervisor){
this.nombre=nombre;
this.apellidos=apellidos;
this.DNI=DNI;
this.direccion=direccion;
this.años_antiguedad=años_antiguedad;
this.telefono=telefono;
this.salario=salario;
this.supervisor=supervisor;
}
public void Imprimir(){
System.out.println("Nombre: "+ nombre + "\n" +
"Apellidos: "+ apellidos + "\n" +
"DNI: "+ DNI + "\n" +
"Direccion: "+ direccion + "\n" +
"Años de antiguedad: "+ años_antiguedad + "\n" +
"Telefono: "+ telefono + "\n" +
"Salario: "+ salario + "\n" +
"Supervisor: "+ supervisor + "\n");
}
public void cambiarSupervisor(Empleado nuevoSupervisor){
this.supervisor=nuevoSupervisor;
}
public void incrementar(double incremento){
this.salario=salario+incremento*salario;
System.out.println("El salario sumandole el incremento es de: " + salario);
}
}
child class
package taller.relaciones.de.clases.parte.pkg2;
/**
*
* @author Usuario
*/
public class Secretario extends Empleado {
String despacho;
int fax;
public Secretario(String despacho, int fax, String nombre, String apellidos, int DNI, String direccion, int años_antiguedad, int telefono, double salario, Empleado supervisor) {
super(nombre, apellidos, DNI, direccion, años_antiguedad, telefono, salario, supervisor);
this.despacho = despacho;
this.fax = fax;
}
@Override
public void Imprimir(){
System.out.println("Despacho: "+ despacho + "\n" +
"Fax: " + fax +"/n");
}
}
Main class
package taller.relaciones.de.clases.parte.pkg2;
/**
*
* @author Usuario
*/
public class TallerRelacionesDeClasesParte2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Secretario secretario1 = new Secretario("Oficina 34",56897513,"Andres","Moreno",1005712016,"Calle 58 norte# 2FN59",5,3214360275,3500000,);
}
}
I would like to clarify that in the main class, in the last comma "," that is next to the parenthesis, the object that I mentioned must go, but I don't know how to fill that field.
The constructor
Secretario
expects an object of type as the last parameterEmpleado
, so you have to create an instance of that class and pass it as a parameter.For example:
I have changed some values, because they are too big to be of type
int
.Also, your methods of displaying data are impractical. In
Secretario
, you should call the method of the parent class, to display the rest of the data. And, it's always better to return a string, and display it in the context, you don't have to print from a class that belongs to the data model.I've added a method
getName()
toEmpleado
, to display the monitor name as well, meaning you should add all the missing getters to the classes.The methods I talked about above would look like this:
In the class
Empleado
:And also this method:
In the class
Secretario
:The output will now look like this:
It should be said that, by naming convention, the methods
Imprimir
should be calledtoString()
, since that is their functionality, to display a representation of the object as a string.I leave you a complete demo of the code:
VER DEMO EN REXTESTER