Demystifying Dart Class Relationships: Implement, Extends, and With
When developing with Dart, we often encounter specific keywords along our journey. As a newcomer to Dart, I vividly recall stumbling upon certain terms that initially sparked fear and left me pondering, "Oh my God, what's that?" One such trio of keywords that perplexed me were "Implement," "Extends," and "With." However, with the wisdom gained from experience as a developer, I'm here to reassure you that encountering such enigmatic concepts is entirely normal and expected. As developers, it's almost inevitable that we encounter unfamiliar territory, leading us to believe that learning is an impossible feat. But fret not, for this is part of the learning process.
Now, let's delve into the crux of the matter: What exactly sets apart these seemingly bewildering keywords and how do they differ from one another? Let's demystify these terms and gain a deeper understanding of their significance in Dart development.
As we know dart is an object-oriented programming language and because of that we can implement and use all concepts that object-oriented has such as inheritance. In dart everything it's an object so everything you create will be an object instantiated from a class and every class you create will inherit, by default, the OBJECT class. So with that we can show you how the EXTENDS keyword works.
EXTENDS:
The extends keyword is employed when we aim to replicate the functionality of an existing class within another class. This practice finds its roots in object-oriented programming through the concept of inheritance. Let's delve into an illustrative example:
领英推荐
In the following scenario, we observe the Animal class featuring a singular method: void walk(). Now, looking at the Dog class, we note that it extends the Animal class. Nestled within, a method named void Bark() resides. Now, direct your attention to the main function. We create an instance of the Dog class, denoted as the variable dog. This instance bestows upon us the ability to invoke both the walk() and bark() methods. Executing the main function we get the following result:
It worked fine but wait, how this is possible? After all, the walk() method resides within the Animal class, not the Dog class. This is where the magic occurs. By utilizing the extends keyword, we signify that the Dog class is a subclass of the Animal superclass. Consequently, the Dog class inherits the entirety of the superclass's contents, exemplifying the essence of inheritance in Dart. It's worth noting that Dart supports single inheritance, signifying that a class can inherit from just one class. This negates the possibility of multiple inheritances, meaning a class cannot inherit from two or more classes simultaneously.
Another cool thing that extends allow us to do is to override one or more methods from the super class, we just need to use the @override annotation. Let's explore this concept further through an illustrative example:
In this example we are using the @override annotation to redefine the walk() method inherited from its superclass, the Animal class. It's crucial to recognize that this modification occurs solely within the context of the Dog class, leaving the method inside the superclass, Animal, untouched. As we execute our main function, we can observe the outcome:
Now you can see how it works in pratice, our method walk() from the Animal class still the same, but when we look at the walk() method from the Dog class we can see that it has changed and we did that by using the @override annotation in the Dog class, cool isn't it?
In summary, the concept of extends serves as our initial foray into comprehending class relationships in Dart. Our subsequent exploration will focus on the implements keyword. I'm eagerly anticipating the opportunity to share more insights within our community.
Desenvolvedor de apps para iOS na Zup Innovation | Desenvolvimento de aplicativos iOS & Android
1 年Congrats brooo
Flutter/Dart developer
1 年show demais