C
C#3mo ago
Nectar

ELI5 : Abstract Class vs Interface

I want to create some models for ORM, and these models have some properties in common such as Id, CreatedDate, CreatedBy, etc. I want to make these properties located in the abstract and later the derived class can be inherited by the abstract class that holds these properties. it should be noted that these properties will definitely have the same implementation. I was thinking that I should implement them in an abstract class because the implementation will always be the same, but I saw a codebase that has a similar structure using an interface instead of an abstract class. so, what should I choose? abstract class or interface? any advice or a more understandable explanation of the two would be useful. thank you!
2 Replies
Nectar
Nectar3mo ago
i think i can make them in one interface like this : interface IBase { int Id { get; set; } DateTime CreatedDate { get; set; } string CreatedBy { get; set; } } cmiiw, but that probably break the DRY principle, cuz they will have the same implementation on every models that inherited from it understandable. i have one question what if there is a scenario where some derived classes will have some methods with the same implementation, but these methods are from some different abstract classes, but due to the single inheritance rule the derived classes cannot inherit from both the abstract classes. what solutions are available? cmiiw, can't abstract classes be instantiated? sorry, I don't really understand, can you give an example? interesting, but you're right, it might be a bit difficult to translate for EF to understand what's going on before you said to use interfaces, does the interface already have an implementation? because what I know, now interfaces can have an implementation, or interfaces only with declarations? hmm understandable, thanks a lot! :Ok:
WEIRD FLEX
WEIRD FLEX3mo ago
remember that you can inherit from only one abstract class at a time
Want results from more Discord servers?
Add your server
More Posts