oop - Different constructor signature for derived class -
is break oop principles (ex, liskov principle), if constructor signature derived class not same base class?
class base { protected x: number; protected y: number; constructor(x: number, y: number) { this.x = x; this.y = y; } } class derived extends base { private text: string; constructor(text: string, x: number, y: number) { super(x, y); this.text = text; } }
no not, because liskov principle talks "contravariance of method arguments , return types in subtype". when this:
foo(bar:base){ //do stuff }
this method expects instance of base
class, not constructor contravariance of methods not apply case.
it break principle if did this, because base
instance not replaced extended
1 :
class base{ foo():string{ return ""; } bar(arg:string){} } class extended extends base{ foo():number{ return 1; } bar(arg:boolean){} }
but not allowed typescript compiler.
Comments
Post a Comment