Technical question?

  • Thread starter Thread starter perspolis
  • Start date Start date
P

perspolis

Hi all
I have a base class that another classes inherit from it.
I want to know when it's good to have interface as parent and when use class
as parent??

and another question ,if all of children classes have a method with same
implementation ,it's better to declare that method with it's implementation
in base class??
thanks in advance
 
perspolis,

When you implement an interface, you are not declaring that you are
inheriting from a base class. Rather, you are declaring that you are
implementing the contract.

And yes, if all derived classes have a child method with the same
implementation, then yes, it is probably better to have that implementation
on the parent class (and maybe make it virtual, in case there can be some
derivation between the child class implementation and the base
implementation).

Hope this helps.
 
"perspolis" <[email protected]> a écrit dans le message de (e-mail address removed)...

| I have a base class that another classes inherit from it.
| I want to know when it's good to have interface as parent and when use
class
| as parent??

Strictly speaking, an interface cannot be a parent to anything except maybe
anther interface.

Classes do not inherit from interfaces, they implement them. Interfaces
define behaviour in much the same way as a totally abstract class. The
difference being that you can only inherit from one class but you can
implement more than one interface.

| and another question ,if all of children classes have a method with same
| implementation ,it's better to declare that method with it's
implementation
| in base class??

When designing classes, it is always best to design them without any thought
of inheritance and, only when you notice commonality, should you then
extract that commonality into a base class. If that method or property
differs in any way from class to class, then think carefully whether that
member sould be declared to be virtual in the base class and to override it
in derived classes.

Joanna
 
Back
Top