Polymorphism with interface and abstract class

Asked

Viewed 1,225 times

1

How do I make a method defined on the interface iDAO is passed on to be implemented by the class ProprietarioDAO, daughter of the abstract class absDAO, implementing 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(){    
    }
}
  • But is that all the abstract class is? If it is, it doesn’t make any sense.

1 answer

1


First you need to think about the way you’re working, it seems wrong to me. I don’t have to give a lot of improvement tips without knowing more about the context, but it would be a good start rethinking 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 only has 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, it will force the daughter 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;
}
  • Thank you very much Linq, now I understand better. The intuition was to import the interface only once... I’m doing a project for the college in Angular with TS to get to know the framework better and try to apply some Design Patterns to see if it works rsrs. Thanks again for your help

  • @Henriquebatista By no means, by coincidence I am also working on a project in Angularjs with Typescript xD

Browser other questions tagged

You are not signed in. Login or sign up in order to post.