Focuse all derived classes to implement a interface specified by the base class

J

Jinsong Liu

I have a class which serve as a base class. I want to focuses all the
classes derived from it implement a interface. Make it a abstract
class is not a option since some XML serialization code need to
initialize the class. is it possible?

thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Jinsong,

Why isn't an abstract class feasible? You can provide a constructor in
the abstract class, and then you can have your derived classes call the
constructor.
 
T

Tom Spink

Jinsong said:
I have a class which serve as a base class. I want to focuses all the
classes derived from it implement a interface. Make it a abstract
class is not a option since some XML serialization code need to
initialize the class. is it possible?

thanks

Hi Jinsong,

The definition of an abstract class is a class which provides a template for
other classes. It does not contain enough information to instantiate it,
as parts are to be implemented by deriving classes. If you are forcing
classes to implement an interface, then you are saying that where you are
deriving from is not complete, i.e. abstract.

It doesn't make sense to force an implementation i.e. define abstract
methods in a non-abstract class, because an instance of that class would
yield methods that are not implemented.

You can define methods as virtual, which means that they _can_ be overridden
by derived classes, but not necessarily:

public class MyBaseClass
{
public virtual void Foo ()
{
}
}

public class MyDerivedClass : MyBaseClass
{
public override void Foo ()
{
// Overridden Implementation.
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top