Polymorphism with interface and abstract class

1

How do I make a method defined in the iDAO interface be passed to be implemented by class ProprietarioDAO , daughter of abstract class absDAO , which implements iDAO ?

import { EntidadeDominio } from "../../models/EntidadeDominio/EntidadeDominio.model";

export interface iDao{    
    salvar( entidade: EntidadeDominio ): String;
    alterar( entidade: EntidadeDominio ): String;
    excluir( entidade: EntidadeDominio ): String;
    consultar( entidade: EntidadeDominio ): EntidadeDominio[];
}
import { iDao } from "./iDao.pattern";

export abstract class absDao implements iDao {    
    salvar;
    alterar;
    consultar;
    excluir;       
}
import { absDao } from "./absDao.pattern";

export class ProprietarioDAO extends absDao {    
    salvar(){    
    }
}
    
asked by anonymous 21.02.2018 / 15:49

1 answer

1

First you need to think hard about the way you are working, it seems wrong to me. I can not give much improvement tip without knowing more about the context, but it would be a good start to rethink the use of this abstract class.

I mean, if you need a class to implement the interface and also extend the abstract class, the right thing would be to have the final class do this and remove these empty methods from the abstract class. And if the abstract class has only these methods, you can simply remove it and make use of the interface only.

For example

export class ProprietarioDAO extends absDao implements iDao { }

and in the abstract class

export abstract class absDao {    
    // Propriedades em comum ou quaisquer outras coisas
}

Anyway, you can simply make the methods of the abstract class abstract too, this will force the child class to implement them.

export abstract class absDao implements iDao {    
    abstract salvar(entidade: EntidadeDominio): String;
    abstract alterar(entidade: EntidadeDominio): String;
    abstract consultar(entidade: EntidadeDominio): EntidadeDominio[];
    abstract excluir(entidade: EntidadeDominio): String;
}
    
21.02.2018 / 15:57