10
I read several contents on this subject, until arriving at this example:
public interface Funcionario{
public void trabalha();
public void recebe(double salario);
}
public abstract class Geek implements Funcionario{
public abstract void trabalha();
public abstract void recebe(double salario);
public void programa(){
System.out.println("import java.io.*; :) ");
}
}
public abstract class BusinessMan implements Funcionario{
public void trabalha(){
System.out.println("$$$");
}
public abstract void recebe(double salario);
}
// programador é um geek e também um funcionário...
public class Programador extends Geek{
private double salario;
public void trabalha(){
super.programa();
}
public void recebe(double salario){
this.salario = salario;
}
}
/* analista também é um geek e um funcionário, mas que trabalha de maneira diferente ao programador*/
public class Analista extends Geek{
private double salario;
public void trabalha(){
super.programa();
supervisionaProjeto();
}
public void recebe(double salario){
this.salario = salario;
}
private void supervisionaProjeto(){
System.out.println("Supervisiona");
}
}
public class Gerente extends BusinessMan{
private double salario;
private final double BONUS = 1000.0d;
public void recebe(double salario){
this.salario = salario + this.BONUS;
}
}
public class Empresa{
public static void main(String[] args){
private Funcionario[] funcionario = new Funcionario[5];
funcionario[0] = new Programador();
funcionario[1] = new Programador();
funcionario[2] = new Analista();
funcionario[3] = new Programador();
funcionario[4] = new Gerente();
// faz alguma coisa...
}
}
Source: http://www.guj.com.br/java/6854-classe-abstrata#38794
I changed the interface Funcionario
by one abastrata class, and I made the other classes, which I used to implement, now extend it. So:
public abstract class Funcionario {
public abstract void trabalha();
public abstract void recebe(double salario);
}
public abstract class Geek extends Funcionario{
@Override
public abstract void trabalha();
@Override
public abstract void recebe(double salario);
public void programa() {
System.out.println("import java.io.*; :) ");
}
}
public abstract class BusinesMan extends Funcionario {
@Override
public void trabalha() {
System.out.println("$$$");
}
@Override
public abstract void recebe(double salario);
}
There was no modification when compiling and running.
My question is: In this specific case changes anything with these changes? Is there any functionality that I will lose or gain?
Note: I am not referring to performance. I am referring to the issue of maintenance or complexity, for example, if later I need to add some more type of employee, there will be difference in the two cases?
It would be a duplicate of http://answall.com/questions/3603/classe-abstraction-x-interface, although the language is not the same?
– bfavaretto
Language really won’t be the differential. Both follow very similar characteristics and the response of one would serve the other language in this case. What can differentiate one from the other is that here is a concrete case to be analyzed. Certainly the C# complements this. If you’re a duplicate, I’d like another opinion.
– Maniero
I read that question and was very much in doubt in asking my question, but I could not get the answer in it for my case. So I asked a new question. Anyway the two address the same theme... Any suggestions so I can edit the question and be more different from the other?
– Franchesco
@Earendul would probably cause more problems because then it could invalidate the answers. What you just said is fundamental to help in the decision. I finished reading the other and really think that here asks and out given something different.
– Maniero
@Earendul From what you say we can not consider duplicate even. I’ll keep our comments here, as a reference, in case anyone in the future thinks they’re duplicate.
– bfavaretto