6

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 {
}

   
   
8
  • 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. Commented Feb 3, 2022 at 16:32
  • the manual is well done typescriptlang.org/docs/handbook/2/classes.html Commented Feb 3, 2022 at 16:57
  • 1
    @ABOS in what way is TS multiple inheritance similar to Java? Commented Feb 3, 2022 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 Commented Feb 3, 2022 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. Commented Feb 3, 2022 at 17:36

1 Answer 1

6

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).

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

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.