consider i have a Customer Class and it got CustomerTypeA, CustomerTypeB, CustomerTypeC as child classes.
Would it be a better design to implement a ICustomer interface to Customer class and create sub type objects (CustomerTypeA, CustomerTypeB, CustomerTypeC)
interface ICustomer {}
class Customer : ICustomer {}
class CustomerTypeA : ICustomer {}
class CustomerTypeB : ICustomer {}
class CustomerTypeC : ICustomer {}
ICustomer obj;
obj = new CustomerTypeB();
or
Create objects of child classes with Customer class object declaration?
class Customer {}
class CustomerTypeA : Customer {}
class CustomerTypeB : Customer {}
class CustomerTypeC : Customer {}
Customer obj;
obj = new CustomerTypeB();
How should i choose which approach to follow?
May be in other words, is it a better design that every parent object to implement from an Interface? what advantage does it bring me?
Thank you
You may use either but people are increasingly favoring composition over inheritance. It offers flexibility at runtime and increased separation of concerns.
To decide what's best in your case you have to examine how the type hierarchy might evolve, what your actual requirements are, how you want to write automated tests etc.