Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must the base class be specified before interfaces when declaring a derived class?

public interface ITest
{
    int ChildCount { get; set; }
}

public class Test
{
}

public class OrderPool : ITest, Test
{
    public int ChildCount
    {
        get;
        set;
    }
}

The error says Base class 'Test' must come before any interfaces. Why is it necessary to extend the class first and then implement the inteface?

like image 865
Mohammad Nadeem Avatar asked Apr 15 '11 10:04

Mohammad Nadeem


People also ask

Why would one create a base class object with reference to the derived class?

One reason for this could be that BaseClass is abstract (BaseClasses often are), you want a BaseClass and need a derived type to initiate an instance and the choice of which derived type should be meaningful to the type of implementation.

When should I use an interface and when should I use a base class?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

What does the derived class inherit from the base class?

The derived class inherits all members and member functions of a base class. The derived class can have more functionality with respect to the Base class and can easily access the Base class. A Derived class is also called a child class or subclass.

How do you declare a derived class?

In the declaration of a derived class, you list the base classes of the derived class. The derived class inherits its members from these base classes. The qualified_class_specifier must be a class that has been previously declared in a class declaration. An access specifier is one of public , private , or protected .


2 Answers

Because the specification says so in section §17.1.2.

like image 161
R. Martinho Fernandes Avatar answered Oct 22 '22 05:10

R. Martinho Fernandes


C# supports only single inheritance, but allows classes to implement multiple interfaces. That being the case, it's much clearer to always have the base class specified in the same place by convention, rather than mixed in with a bunch of interfaces.

Regardless of convention, the specification mandates that this is the case anyway, which is why you're seeing that error.

Remember, there's nothing in the specification that says all of your interfaces have to be named with a capital "I". - that's just convention. So if your class implemented interfaces that didn't follow that convention, and if the specification allowed you to specify the base class and interfaces in any order, you wouldn't be able to easily tell which identifier was the base class and which were interfaces. Example:

class MyDerivedClass : A, B, C, D, E   // which is the base class?  
{
...
}
like image 23
razlebe Avatar answered Oct 22 '22 07:10

razlebe