Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface vs Multiple Inheritance In C#

I have a set of Class A and Class B both have Some properties. and another Class C which has their own properties.

Whenever i create a instance of class C i want to access all properties of all three classes with objClassC.

How can i Achieve this in C#?

I m facing two problem :-

  1. I can not inherit both the classes A, B in Class C (C# doesn't Support Multiple Inheritance)
  2. if i use Interface instead of Class A, B (In Interface we can not Contains Fields)
like image 435
Shashank Avatar asked May 04 '12 11:05

Shashank


People also ask

Is it better to inherit from an interface or class?

In C#, you can only inherit from one class but multiple interfaces. This would be yet another reason to choose interface over inheritance. Get the idea? Those are good simple rules of thumb. Inheritance is, in my opinion, the better approach when it is possible.

What is multiple inheritance in C?

In Multiple inheritance, one class can have more than one superclass and inherit features from all its parent classes. As shown in the below diagram, class C inherits the features of class A and B. But C# does not support multiple class inheritance.

What is multiple inheritance in Java?

In Multiple inheritance, one class can have more than one superclass and inherit features from all its parent classes. As shown in the below diagram, class C inherits the features of class A and B.

Which class inherits from both classes a and B?

Class C inherits from both classes A and B. It is an example of multiple inheritance. Class C definition is shown below − In main () function, an object obj of class C is defined. The constructors of Class A, B and C are automatically called and their contents are displayed.


Video Answer


2 Answers

Consider how the properties are exposed differently to the client when using inheritance vice composition.

Inheritance:

    var myCclass = new Cclass;
    myClass.propertyA;
    myClass.propertyB;
    myClass.propertyC;
    // and so on

Composition:

 var myCclass = new Cclass;
    myCclass.bClass.propertyB;
    myCclass.aClass.propertyA;
    myCclass.propertyC;

Inheritance gives a cleaner API - a good thing.

Composition requires me to know something about the internal structure of the class - not such a good thing. This violates the law of demeter - better known as the principle of least knowledge. You can get around this by having Cclass properties that one-for-one expose/return the Bclass & Aclass properties - and your Bclass & Aclass references would then be private or protected in Cclass. AND Cclass has total control over what's exposed rather than depending on A & B to not have public stuff you don't was exposed.

I agree with @AlejoBrz, interfaces is not appropriate here.

I also give a nod to "prefer composition over inheritance." But this is a guideline, not a hard and fast rule.

like image 191
radarbob Avatar answered Sep 21 '22 15:09

radarbob


Why don't you contain instance of Class A and Class B inside Class C. Use Composition

class C
{
//class C properties
public A objA{get;set;}
public B objeB{get;set;}
}

Then you can access

C objc = new C();
objc.objA.Property1 = "something";
objc.objB.Property1 = "something from b";

check out the article Composition vs Inheritance

EDIT:

if i use Interface instead of Class A, B (In Interface we can not Contains Fields)

Well, interfaces can't contain fields, if you define one, you will get compilation error. But interfaces can contain properties with the exception that you can't specify the access specifiers, as all elements of the interface are considered public. You can define properties for Interface 'A' and 'B' as:

public interface IA
{
     int Property1 { get; set; }
}


public interface IB
{
    int Property2 { get; set; }
}

Then you can implement them in the class C like:

public class C : IA, IB
{
    public int Property1 { get; set; }
    public int Property2 { get; set; }
}

Later you can use them as:

C objC = new C();
objC.Property1 = 0;
objC.Property1 = 0;
like image 25
Habib Avatar answered Sep 20 '22 15:09

Habib