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 {}
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 {}
Short version
extends
means:The new class is a child. It gets benefits coming with inheritance. It has all properties, methods as its parent. It can override some of these and implement new, but the parent stuff is already included.
implements
means:The new class can be treated as the same "shape", while it is not a child. It could be passed to any method where the
Person
is required, regardless of having different parent thanPerson
More ...
In OOP (languages like C#, Java) we would use
extends
to profit from inheritance (see wiki). Small cite:implements
will be more for polymorphism (see wiki). Small cite:So, we can have really different inheritance tree of our
class Man
.but if we also declare that we can pretend to be a different type -
Person
:.. then we can use it anywhere, where the
Person
is required. We just have to fulfill Persons's"interface"
(i.e. implement all its public stuff).implement
other class? That is really cool stuffJavascript nice face (one of the benefits) is the built-in support of the Duck typing (see wiki). Small cite:
So, in Javascript, if two different objects... would have one similar method (e.g.
render()
) they can be passed to a function which expects it:To not lose that - we can in Typescript do the same - with more typed support. And that is where
has its role, where it makes sense
In OOP languages as
C#
... no way to do that...Also The documentation should help here:
So, while
extends
means - it gets all from its parentimplements
in this case is almost like implementing an interface. Child object can pretend that it is parent... but it does not get any implementationGreat Answer from @nitzan-tomer! Helped me a lot... I extended his demo a bit with:
And how they behave in functions expecting an IPoint type.
So what I've learned so far and been using as a thumb-rule: if you're using classes and methods expecting generic types, use interfaces as the expected types. And make sure the parent or base-class uses that interface. That way you can use all subclasses in those as far as they implement the interface.
Hier the extended demo
In typescript (and some other OO languages) you have classes and interfaces.
An interface has no implementation, it's just a "contract" of what members/method this type has.
For example:
Instances who implement this
Point
interface must have two members of type number:x
andy
, and one methoddistance
which receives anotherPoint
instance and returns anumber
.The interface doesn't implement any of those.
Classes are the implementations:
(code in playground)
In your example you treat your
Person
class once as a class when you extend it and once as an interface when you implement it.Your code:
Has a compilation error saying:
And that's because interfaces lack implementation.
So if you
implement
a class then you only take its "contract" without the implementation, so you'll need to do this:(code in playground)
Bottom line is that in most cases you want to
extend
another class and not toimplement
it.