jeudi 30 décembre 2021

Is it a better design to inherit from an abstract class or from an interface if the child classes have a common property?

I have two classes B and C which share a common property a of type D.

class B
{
  public D a {get;set;}
  public E b {get;set;}
}

class C
{
  public D a {get;set;}
  public F c {get;set;}
}

I could make a

abstract class A 
{
  public D a {get;set;}
}

from which B and C inherits.

class B:A
{
  public E b {get;set;}
}

class C:A
{
  public F c {get;set;}
}

I also know that i will have a service with a method GetValue which returns an object of type A.

interface Service
{
  A GetValue();
}

The class A could also be an interface but then the property a had to be implemented double by B and C. The question is should I use interface or abstract for implementig A? To inherit from abstract class just for the reason that two classes have a common property seem to be not a good practice, but I am not sure.

Aucun commentaire:

Enregistrer un commentaire