1

If I have two classes, is it possible to create a third class that extends both of them?

abstract class A {
    name
    age
    protected abstract print()
}
        
abstract class B {
    name
    age
}
        
// class C needs to inherit from both of the above classes

class C extends A , B {
}

   
   
Tom
  • 1,158
  • 6
  • 19
daniel wix
  • 53
  • 1
  • 6
  • 1
    It is not possible to extend two or more classes at the same time. Multiple inheritance is not allowed in Java. You can use interfaces. In this case the class c can implement the interface A and B, for example. – Dacleoz Feb 03 '22 at 16:32
  • the manual is well done https://www.typescriptlang.org/docs/handbook/2/classes.html – dcolazin Feb 03 '22 at 16:57
  • 1
    @ABOS in what way is TS multiple inheritance similar to Java? – VLAZ Feb 03 '22 at 17:03
  • 1
    @VLAZ man i don't know what you trying to prove here it's obvious that tag was added by mistake – daniel wix Feb 03 '22 at 17:23
  • 1
    @danielwix ABOS edited the title of your question to add "similar to Java" therefore linking your query back to the apparently unrelated language. Also, it's not really "obvious" it was a mistake. We get questions with weird mixes of tags every day. Yes, a lot are a mistake but without the ability to read the mind of the question asker, we don't *know* whether they've added a tag because they meant to or not. When in doubt, I prefer to ask. Who knows, maybe you wanted to emulate some Java construct you had but in TS. We get similar questions also on a daily basis. – VLAZ Feb 03 '22 at 17:36
  • all good @VLAZ but for a simple question i am getting downvotes rather than answers haha – daniel wix Feb 03 '22 at 18:03
  • [It's not a simple problem.](https://stackoverflow.com/q/225929/) It's one of the funamental issues in OOP design. You can't just be solved. Like that. Some languages have "solved" it by forbidding it altogether. Java *used to* be like that. With default method bodies it is in some ways possible to inherit from multiple unrelated taxonomies, ***however***, it is a compilation error if both ancestors declare the same method. Thus there isn't really a "solved" multiple inheritance even then in Java, just loosened restrictions around it. Moreover, with the example you've shown, there is no – VLAZ Feb 03 '22 at 18:16
  • reason to use multiple inheritance - presumably both `A` and `B` can have a common ancestor, thus `C` would not need to inherit from both. Or `C` can simply inherit from `A`. There is little reason for `C` to be a subtype of `B` here. And if it *does* then your example does not seem representative. In JS (and TS after bypassing some of it) it's possible to "splice" multiple ancestors into the prototype chain which is *sort of* like multiple inheritance. But doesn't "solve" it, either. It's brute forcing it in. So, hard to solve a design issue without a real problem. – VLAZ Feb 03 '22 at 18:16

1 Answers1

4

Yes, depending on the requirements there are several ways to do this. Given the classes A and B:

class A {
  constructor(public i: number){}
  foo() {
    return 123;
  }
}

class B {
  constructor(public name: string){}
  bar() {
    return "Hello from B!";
  }
}

you can do any of the following and have it just work (for some value of just-work):

  1. Inherit one class, implement the other as an interface (in TypeScript any class may be treated as an interface):

    class Cv1 extends B implements A { /* implement A-as-interface here */ }
    
  2. Create a type alias for A & B and implement the type alias (e. g. treat both classes as interfaces):

    type AandB = A & B;
    class Cv2 implements AandB {
      constructor(public i: number, public name: string) {}
      foo(...args: []) {
        return A.prototype.foo.apply(this, args);
      }
      bar() {
        return "Cv2 says GOODBYE!";
      }
    }
    
  3. Mix both classes together and inherit from the mixed class:

    // Exercise for the reader: do it without any
    function mixin(a: any, b: any): any {
      // Choice: Should isA apply to either mixin? Should it apply to neither?
      const aNotB = Object.defineProperties(Object.create(a.prototype), Object.getOwnPropertyDescriptors(b.prototype));
      const shadowClass: any = function shadowClass(){}
      shadowClass.prototype = aNotB;
      class mixinImpl extends shadowClass {}
      return mixinImpl;
    }
    
    class Cv3 extends mixin(A, B) { }
    

Caveat Emptor

There are limitations with this technique. For example, if B has a prototype chain of its own (e. g. B extends BPrime and BPrime extends BPrimordial, etc.) then none of the methods and properties of BPrime and BPrimordial will be copied over (you can walk the prototype chain yourself at class construction time to work around this if that's what you want to do). Also, instanceof will not work with any of the implements or .assign techniques (e. g. anInstanceOfC instanceof B will be false using any of the techniques above).

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293