Reputation: 895
https://dart.dev/guides/language/language-tour#implicit-interfaces
I've seen code that uses "implements", so I'm looking into "implements". But I can't really tell the difference from extends by looking at the official docs.
Looking at the sample code in the official documentation (page above), it looks like it is just doing what it can do with extends with implements. Then I wonder if it should use "extends". I think I've understood about inheritance (extends) and mixins (with) so far. The word "interface" is also mentioned in the Dart documentation, but there is a clear definition of "interface". I can't even find where it is. I don't have much knowledge about interfaces in other languages, so it's hard to get an image.
What exactly is the difference between inheritance and implementation? Because of the difference, when do you use the inheritance "extends" and when do you use the implementation "implements"?
Is there somewhere in the sample that makes a clear difference?
Upvotes: 4
Views: 2229
Reputation: 196
another definition that may help you got the point.. Use extends make class inherits (extends) his parent, where you extend(have) his properties even if you don't override parent class's properties you can use the parent class's properties, methods beside his own properties and methods, also you should validate that this class(child) commits it's parent class and doesn't break the restrictions of the parent .. where as
Use implements when you want the class just restrict to the abstract class properties but you have your own implementation for these properties.
Upvotes: 0
Reputation: 618
If you just want to understand how they are different concept-wise then,
You extend a class. Think of it as you extended your father or your father is your base class.
You implement an interface(which is just a purely abstract class). Like Ferrari implements a car.
An interface cannot have an instance. For example - Have you seen any CAR? The answer is no, you have seen types of car i.e, Ford, Toyota which implements the CAR so the car acts as an interface which other companies or you can say, classes(Ferrari) implements.
You have to implement every feature of a car to be called a car that's why every method of an interface needs to be implemented and we say x "implements" y.
In extending you can override something and skip another , suppose your nose may look like your father but your ears do not.
Think of an interface as a skeleton or just an empty class.
Upvotes: 1
Reputation: 318
Besides other explanations, dart does not let developers to use multiple inheritance so there can be only one extends, however there can be more than one implements.
Therefore a class can be subtype of only one class(except parents of parent class) but interfaces defines behaviour of the class which implements it and one class can have different behaviours.
A simple analogy is from animals. If we assume there is different behaviours for animals like swimming interface as ISwimmer, running interface as IRunner and flying interface as IFlyer. For example a Fish class is an Animal. Which makes it extends Animal class. Additionally a Fish has behaviour of swimming so it implements ISwimmer interface in that purpose.
Upvotes: 2
Reputation: 1190
Given class MyClass
:
MyClass extends Foo
is classic inheritance. MyClass
is a Foo
MyClass implements Bar
is declaring that the implementer conforms to the Bar
interface. MyClass
"looks" like a Bar
MyClass with Batz
is "mixing" in the interface and implementation. MyClass
"acts" like a Batz
.MyClass
can implement and mixin as many interfaces as needed (some limitations apply) but can only extend from one interface.
Upvotes: 10