1.

How Can You Force Derived Classes To Provide New Method Implementations For Virtual Methods?

Answer»

Abstract classes can be used to FORCE derived classes to provide NEW method IMPLEMENTATIONS for virtual methods. An example is SHOWN below.
public class BaseClass
{
public virtual void Method()
{
// Original Implementation.
}
}
public abstract class AbstractClass : BaseClass
{
public abstract OVERRIDE void Method();
}
public class NonAbstractChildClass : AbstractClass
{
public override void Method()
{
// New implementation.
}
}

When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method. If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A class inheriting an abstract method cannot access the original implementation of the method. In the above example, Method() on class NonAbstractChildClass cannot call Method() on class BaseClass. In this way, an abstract class can force derived classes to provide new method implementations for virtual methods.

Abstract classes can be used to force derived classes to provide new method implementations for virtual methods. An example is shown below.
public class BaseClass
{
public virtual void Method()
{
// Original Implementation.
}
}
public abstract class AbstractClass : BaseClass
{
public abstract override void Method();
}
public class NonAbstractChildClass : AbstractClass
{
public override void Method()
{
// New implementation.
}
}

When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method. If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A class inheriting an abstract method cannot access the original implementation of the method. In the above example, Method() on class NonAbstractChildClass cannot call Method() on class BaseClass. In this way, an abstract class can force derived classes to provide new method implementations for virtual methods.



Discussion

No Comment Found