Net-informations.com
SiteMap  | About    

Multiple inheritance in C#

Multiple inheritance in C#

Multiple inheritance allows programmers to create classes that combine aspects of multiple classes and their corresponding hierarchies. For ex. the C++ allows you to inherit from more than one class

In C#, the classes are only allowed to inherit from a single parent class, which is called single inheritance. But you can use interfaces or a combination of one class and interface(s), where interface(s) should be followed by class name in the signature.

Ex:

Class FirstClass { } Class SecondClass { }

interface X { } interface Y { }

You can inherit like the following:

class NewClass : X, Y { }

In the above code, the class "NewClass" is created from multiple interfaces.

class NewClass : FirstClass, X { }

In the above code, the class "NewClass" is created from interface X and class "FirstClass".

WRONG:

class NewClass : FirstClass, SecondClass { }

C# doesn't support Multiple inheritance

The above code is wrong in C# because the new class is created from class "FirstClass" and class "SecondClass". The reason is that C# does not allows multiple inheritance.

.Net Interface

.Net Interface

An interface looks like a class, it contain only the declaration of the members but has no implementation. It provides a contract specifying how to create an Object, without caring about the specifics of how they do the things. An Interface is a reference type and it included only the signatures of methods, properties, events or indexers. In order to implement an interface member, the corresponding member of the implementing class should be public, non-static, and have the same name and signature as the interface member. Click here to know more about.... C# Interfaces .







net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.