Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to partially implement a contract in an abstract base class?

Tags:

c#

.net

I have a interface as follows

 public interface IX
    {
        void MethodA();
        void MethodB();
    }

I have two method contracts in the interface MethodA and MethodB. I will define set of classes that will implement the above interface. Out of these two methods MethodA is common for all the types that will implement the interface. I can define a abstract class as follows

public abstract class XBase:IX
    {
        public void MethodA()
        { 
            // Common behaviour implementation
        }

        public abstract void MethodB();
    }

And inherit this class to all the types that need to implement the above interface. It works.

But here in the abstract class i add 'public abstract void MethodB();'. It looks like repetition of the MethodB contract.

Why C# does not permit partial interface implementation if the class is abstract?. The above interface has only two methods. suppose one interface has 10 methods and 5 are common functionality and 5 are not, we are forced to add the 5 methods that are not common in the abstract class?

like image 884
RAM Avatar asked Jul 22 '10 18:07

RAM


People also ask

Can abstract class be partially implemented?

An abstract class is a special type of class that cannot be instantiated. An abstract class is designed to be inherited by subclasses that either implement or override its methods. In other words, abstract classes are either partially implemented or not implemented at all.

Can abstract classes have implementations?

Abstract class in Java is similar to interface except that it can contain default method implementation. An abstract class can have an abstract method without body and it can have methods with implementation also. abstract keyword is used to create a abstract class and method.

Can a class partially implement an interface?

A class cannot implement an interface partially. [C]. An interface can contain static methods.

How can we get implemented method in abstract class?

To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass. A subclass must override all abstract methods of an abstract class. However, if the subclass is declared abstract, it's not mandatory to override abstract methods.


1 Answers

Because the C# language specification says so. Chapter 13.4.7:

Like a non-abstract class, an abstract class must provide implementations of all members of the interfaces that are listed in the base class list of the class.

Why the C# designers chose to specify the language like this is probably best answered by Eric Lippert. I'd personally guess at reducing the odds that unintended method hiding occurs, producing error messages that are very hard to interpret. I would personally have been more comfortable requiring the use of the overrides keyword for the interface method implementation. But they chose not to.

like image 154
Hans Passant Avatar answered Oct 01 '22 03:10

Hans Passant