Sunday, November 28, 2010

Derive an Abstract class from an Abstract class

Question : What happen when you derive a abstract class from another abstract class? Is it mandatory to implement all the methods of abstract base class in abstract derive class?


Answer : No, it is not mandatory to implement all the methods of abstract base class in abstract derive class.

All abstract method of an abstract class should be public. You can not override any method if it is not abstract or virtual.
You can not define an abstract method as a private in abstract class will give compile time error.

abstract class Base
{
public abstract void BaseMethod();
}

abstract class Child:Base
{
public abstract void ChildMethod();
}

//If any class derived from that abstract child class than it must implement all the methods of both classes.

class Client: Child
{
public override void BaseMethod()
{
Console.Writeline("Base Method");
}


public override void ChildMethod()
{
Console.Writeline("Child Method");
}

}

If you have implemented the abstract method of abstract Base class in abstract Child class than there is no need to implement that method in Client class. But if you want you can override it in Client too.

No comments:

Followers

Link