Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple inheritance workarounds

Tags:

I'm trying to discover a pattern for combining multiple interfaces into one abstract class. Presently I can combine multiple interfaces via implements, but an interface cannot declare a constructor. When I must introduce a constructor I'm forced to use an abstract class. When I use a abstract class I must re-declare the entire composite interface! Surely I'm missing something?

interface ILayerInfo {     a: string; }  interface ILayerStatic {     b(): string; }  class Layer implements ILayerInfo, ILayerStatic {     constructor(info: ILayerInfo);     a: string;     b(): string; } 

ANSWER: Use new:

interface Layer extends ILayerInfo, ILayerStatic {     new(info: ILayerInfo); }  // usage: new Layer({ a: "" }); 
like image 644
Corey Alix Avatar asked Jul 25 '13 18:07

Corey Alix


People also ask

What is problem with multiple inheritance?

Multiple inheritance has been a controversial issue for many years, with opponents pointing to its increased complexity and ambiguity in situations such as the "diamond problem", where it may be ambiguous as to which parent class a particular feature is inherited from if more than one parent class implements said ...

What is meant by multiple inheritance?

You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance.

How does interface solve the problem of multiple inheritance?

Interfaces contain only public constant definitions and method headers. Interfaces can only extend other interfaces, not classes. Interfaces can extend multiple interfaces, and classes can implement multiple interfaces.

Why does java doesn't support multiple inheritance?

Java doesn't support multiple inheritances in classes because it can lead to diamond problem and rather than providing some complex way to solve it, there are better ways through which we can achieve the same result as multiple inheritances.


1 Answers

Declaring a constructor on the same interface as the instance members doesn't really make much sense -- if you're going to pass in a type dynamically to use in a constructor, it's the static side of the class that would be restricted. What you would want to do is probably something like this:

interface Colorable {     colorize(c: string): void; }  interface Countable {     count: number; }  interface ColorCountable extends Colorable, Countable { }  interface ColorCountableCreator {     new(info: {color: string; count: number}): ColorCountable; }  class ColorCounted implements ColorCountable {     count: number;     colorize(s: string) { }     constructor(info: {color: string; count: number}) {         // ...     } }  function makeThings(c: ColorCountableCreator) {     var results: ColorCountable[];     for(var i = 0; i < 10; i++) {         results.push(new c({color: 'blue', count: i}));     }     return results; }  var items = makeThings(ColorCounted); console.log(items[0].count); 

See also How does typescript interfaces with construct signatures work?

like image 132
Ryan Cavanaugh Avatar answered Sep 18 '22 02:09

Ryan Cavanaugh