Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something like a class that can be implemented?

I'd like to write a
class X (this) which
inherits from A (base) can
execute the methods of B (?) and must
implement the members of C (interface).

Implementing A and C are not a problem. But since X cannot derive from multiple classes it seems impossible to have X inherit the logic of A and B. Note that A is the very important base class and B is almost a interface but contains executable behaviour. The reason why I don't want B to be an interface is because the behaviour is the same for every class that inherits or implements it.

Do I really must declare B as an interface and implement the exact same 10 lines of code for each X that needs the behaviour of B?


2 months later
I am currently learning C++ for using it in UE4 (Unreal Engine 4).
Since C++ is a lot less strict than C# it actually contains a pattern implementation idom term that describes this behaviour: These are called mixins.

You can read a paragraph about the C++ mixin here on page 9 (second paragraph).

like image 386
Noel Widmer Avatar asked Apr 21 '15 09:04

Noel Widmer


People also ask

Can we implement classes in C?

This document describes the simplest possible coding style for making classes in C. It will describe constructors, instance variables, instance methods, class variables, class methods, inheritance, polymorphism, namespaces with aliasing and put it all together in an example project.

What are .NET classes?

A class defines a type of object, but it is not an object itself. An object is a concrete entity based on a class, and is sometimes referred to as an instance of a class. Objects can be created by using the new keyword followed by the name of the class that the object will be based on, like this: C# Copy.

Can interface inherit class in C#?

Interfaces can inherit from one or more interfaces. The derived interface inherits the members from its base interfaces. A class that implements a derived interface must implement all members in the derived interface, including all members of the derived interface's base interfaces.

Why Multiple inheritance is not possible in C#?

C# compiler is designed not to support multiple inheritence because it causes ambiguity of methods from different base class. This is Cause by diamond Shape problems of two classes If two classes B and C inherit from A, and class D inherits from both B and C.


2 Answers

Do I really must declare B as an interface and implement the exact same 10 lines of code for each X that needs the behaviour of B?

Yes and no. You do need to make B an interface. But common method implementations should not be duplicated across all implementations of the interface. Instead, they should go into a class extension for interface B:

public interface B {
    void MethodX();
    void MethodY();
}
public static class ExtensionsB {
    public static void MethodZ(this B b) {
        // Common implementations go here
    }
}

Extension methods provide a way to share implementations "horizontally", without having your classes inherit from a second class. Extension methods behave as if they were regular methods of the class:

class X : A, B {
    public void MethodX() {...}
    public void MethodY() {...}
}
public static void Main(string[] args) {
    var x = new X();
    x.SomeMethodFromA();
    x.MethodX(); // Calls method from X
    x.MethodY(); // Calls method from X
    x.MethodZ(); // Calls method from ExtensionsB
}
like image 108
Sergey Kalinichenko Avatar answered Oct 29 '22 15:10

Sergey Kalinichenko


I think your best bet would be to require an instance of B in the constructor of A and then expose or call the methods of B as required:

public class X : A, C
{
    private readonly B _b;

    public X(B b)
    {
        _b = b;
    }
}

You'll find a lot of information on this sort of approach if you look up Composition over inheritance

like image 28
RagtimeWilly Avatar answered Oct 29 '22 15:10

RagtimeWilly