gotchatypescriptCritical
What's the difference between 'extends' and 'implements' in TypeScript
Viewed 0 times
typescriptimplementsextendsandbetweenthedifferencewhat
Problem
I would like to know what Man and Child have in common and how they differ.
class Person {
name: string;
age: number;
}
class Child extends Person {}
class Man implements Person {}
Solution
Short version
The new class is a child. It gets benefits coming with inheritance. It has all the properties and methods of its parent. It can override some of these and implement new ones, but the parent stuff is already included.
The new class can be treated as the same "shape", but it is not a child. It could be passed to any method where
More ...
In OOP (languages like C# or Java) we would use
... Inheritance in most class-based object-oriented languages is a mechanism in which one object acquires all the properties and behaviours of the parent object. Inheritance allows programmers to: create classes that are built upon existing classes ...
... polymorphism is the provision of a single interface to entities of different types...
So we can have a completely different inheritance tree for our class
but if we also declare that
...then we can use it anywhere
Javascript's nice face (one of the benefits) is built-in support for duck typing.
"If it walks like a duck and it quacks like a duck, then it must be a duck."
So, in Javascript, if two different objects have one similar method (e.g.
To not lose that in Typescript, we can do the same with more typed support. And that is where
has its role, where it makes sense.
In OOP languages as
The documentation should help here:
Interfaces Extending Classes
When an interface type extends a class type it inherits the members of
the class but not their implementations. It is as if the interface had
declared all of the members of the class without providing an
implementation. Interfaces inherit even the private and protected
members of a base class. This means that when you create an interface
that extends a class with private or protected members, that interface
type can only be implemented by that class or a subclass of it.
This is useful when you have a large inheritance hierarchy, but want
to specify that your code works with only subclasses that have certain
properties. The subclasses don’t have to be related besides inheriting
from the base class. For example:
So, while
extendsmeans:
The new class is a child. It gets benefits coming with inheritance. It has all the properties and methods of its parent. It can override some of these and implement new ones, but the parent stuff is already included.
implementsmeans:
The new class can be treated as the same "shape", but it is not a child. It could be passed to any method where
Person is required, regardless of having a different parent than Person.More ...
In OOP (languages like C# or Java) we would use
extends to profit from inheritance.... Inheritance in most class-based object-oriented languages is a mechanism in which one object acquires all the properties and behaviours of the parent object. Inheritance allows programmers to: create classes that are built upon existing classes ...
implements will be more for polymorphism.... polymorphism is the provision of a single interface to entities of different types...
So we can have a completely different inheritance tree for our class
Man:class Man extends Human ...but if we also declare that
Man can pretend to be the Person type:class Man extends Human
implements Person ......then we can use it anywhere
Person is required. We just have to fulfil Person's "interface" (i.e. implement all its public stuff).implement other class? That is really cool stuffJavascript's nice face (one of the benefits) is built-in support for duck typing.
"If it walks like a duck and it quacks like a duck, then it must be a duck."
So, in Javascript, if two different objects have one similar method (e.g.
render()) they can be passed to a function which expects it:function(engine){
engine.render() // any type implementing render() can be passed
}To not lose that in Typescript, we can do the same with more typed support. And that is where
class implements classhas its role, where it makes sense.
In OOP languages as
C#, no way to do that.The documentation should help here:
Interfaces Extending Classes
When an interface type extends a class type it inherits the members of
the class but not their implementations. It is as if the interface had
declared all of the members of the class without providing an
implementation. Interfaces inherit even the private and protected
members of a base class. This means that when you create an interface
that extends a class with private or protected members, that interface
type can only be implemented by that class or a subclass of it.
This is useful when you have a large inheritance hierarchy, but want
to specify that your code works with only subclasses that have certain
properties. The subclasses don’t have to be related besides inheriting
from the base class. For example:
class Control {
private state: any;
}
interface SelectableControl extends Control {
select(): void;
}
class Button extends Control implements SelectableControl {
select() { }
}
class TextBox extends Control {
select() { }
}
// Error: Property 'state' is missing in type 'Image'.
class Image implements SelectableControl {
private state: any;
select() { }
}
class Location {
}So, while
extendsmeans it gets all from its parent
implementsin this case it's almost like implementing an interface. A child object can pretend that it is its parent... but it does not get any implementation.
Code Snippets
class Man extends Human ...class Man extends Human
implements Person ...function(engine){
engine.render() // any type implementing render() can be passed
}class implements classclass Control {
private state: any;
}
interface SelectableControl extends Control {
select(): void;
}
class Button extends Control implements SelectableControl {
select() { }
}
class TextBox extends Control {
select() { }
}
// Error: Property 'state' is missing in type 'Image'.
class Image implements SelectableControl {
private state: any;
select() { }
}
class Location {
}Context
Stack Overflow Q#38834625, score: 391
Revisions (0)
No revisions yet.