5

I have a theoretical question concerning how to deal with the following scenario in a language which does not allow multiple inheritance.

Imagine I have a base class Foo and from it I am wishing to create three sub-classes:

  • Class Bar inherits Foo and implements functionality "A"
  • Class Baz inherits Foo and implements functionality "B"
  • Class Qux inherits Foo and implements functionalities "A" and "B"

Imagine that the code to implement functionalities "A" and "B" is always the same. Is there a way to write the code for "A" and "B" only once, and then have the appropriate classes apply (or "inherit") it?

5
  • Any particular language? Java? C#? Something else? Commented May 27, 2012 at 13:00
  • Why arent u implementing the same functionality within your FOO class, and just marking A/B as virtual/overridable ? Commented May 27, 2012 at 13:01
  • @Tudor Was thinking in C# but any OOP language which doesn't allow multiple inheritance is fine. Commented May 27, 2012 at 13:02
  • @YavgenyP - Let's say that FOO and descendants serve to connect to a server, and functionality A provides a method which adds on to the standard FOO connection mechanism and allows authenticated requests. Overriding the connecting method would solve the problem, but I would have to do the override in both BAR and QUX. If the code is the same, I end up with two copies. Was hoping there was a more elegant solution that I was unaware of. Commented May 27, 2012 at 13:10
  • 1
    @Mr_Creosote: I've put the C# and Java tags just for the sake of getting views, since your original two tags are not that popular. Commented May 27, 2012 at 13:17

4 Answers 4

3

Well the only way I can see you achieving this in C#/Java is by composition. Consider this:

class Foo {

}

interface A {
    public void a();
}

interface B {
    public void b();
}

class ImplA implements A {
    @Override
    public void a() {
        System.out.println("a");
    }
}

class ImplB implements B {
    @Override
    public void b() {
        System.out.println("b");
    }
}

class Bar extends Foo {
    A a = new ImplA();

    public void a() {
        a.a();
    }
}

class Baz extends Foo {
    B b = new ImplB();

    public void b() {
        b.b();
    }       
}

class Qux extends Foo {

    A a = new ImplA();
    B b = new ImplB();

    public void b() {
        b.b();
    }

    public void a() {
        a.a();          
    }       
}

Now Qux has both the functionality of Foo via normal inheritance but also the implementations of A and B by composition.

Sign up to request clarification or add additional context in comments.

Comments

3

The more general term for this is a Mixin. Some languages provide support out of the box, such as Scala and D. There are various ways to achieve the same results in other languages though.

One way you can create a pseudo-mixin in C# is to use empty interfaces and provide the methods with extension methods.

interface A { }
static class AMixin {
    public static void aFunc(this A inst) {
        ... //implementation to work for all A.
    }
}

interface B { }
static class BMixin {
    public static void bFunc(this B inst) {
        ...
    }
}

class Qux : Foo, A, B {
    ...
}

Comments

1

This is achievable in languages providing traits (here: ):

class Foo {
    def fooM() {}
}

trait A {
    def aFunc() {}
}

trait B {
    def bFunc() {}
}

class Bar extends Foo with A {}

class Baz extends Foo with B {}

class Qux extends Foo with A with B {}

Because Scala runs on top of Java (having neither multiple inheritance nor traits) it is translated into something like this (simplified) - which might be a hint how to implement it in Java/C# manually:

class Foo {
}

interface A {
    void aFunc();
}

interface B {
    void bFunc();
}

class Bar extends Foo implements A {

    public void aFunc() {
        $A.aFunc();
    }
}

class Baz extends Foo implements B {

    public void bFunc() {
        $B.bFunc();
    }
}

class Qux extends Foo implements A, B {

    public void aFunc() {
        $A.aFunc();
    }

    public void bFunc() {
        $B.bFunc();
    }
}

class $A {

    public static void aFunc() {}
}

class $B {

    public static void bFunc() {}
}

Comments

1

There are several ways to do something like this. More specifically, if we abandon the inheritance aspect for a moment, there are ways to introduce the same unit of functionality to different classes, while writing the unit only once.

Okay, I love AOP Frameworks, and they exist for many languages (C# and Java having several). AOP Frameworks basically allow you add self-contained functionality into different classes throughout your inheritance structure.

For C#, you have PostSharp and for Java you have AspectJ, among many others.

Many AOP frameworks allow 'hijacking' or 'overriding' method calls without using inheritance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.