"Demystifying Dart Class Relationships: Implement, Extends, and With Explained"

"Demystifying Dart Class Relationships: Implement, Extends, and With Explained"

Welcome back for the second installment in our three-article series. In our previous discussion, we explored the 'Extends' keyword, and I trust you gained a comprehensive grasp of its significance in Dart development. Today, we're delving into the realm of the 'Implement' keyword, unraveling its meaning and uncovering how it can be a valuable asset in our coding endeavors.



Implement:

Have you ever heard of the concept of Interfaces? Let me provide a quick explanation of what an Interface is. An Interface acts as a contract between classes. In Dart, there is no dedicated 'interface' keyword; instead, all classes are implicit interfaces. An interface enforces that any class implementing it must provide a specific set of public fields and methods. Often, we define an abstract class as an interface, and then we can implement that interface using another concrete class. One of the reasons we use an abstract class is that it can have abstract methods (methods with empty bodies). Abstraction is a fundamental concept in object-oriented programming (OOP) languages, and Dart being an OOP language, we can make use of this concept by using the 'implements' keyword. So, let's dive into some coding to gain a full understanding of how 'implements' works.

In this example, we can observe the distinction between the 'extends' and 'implements' keywords. The class 'Shark' extends the 'MarineAnimal' class. When utilizing the 'extends' keyword, there's no obligation to override the inherited class's definition; we can simply utilize the existing definition from the parent class. However, when we use the 'implements' keyword, we are required to redefine all methods from the interface, as evident from the error message. Now, let's proceed to implement our 'Whale' class:

Now that we have implemented the 'Swim' method, our error message no longer occurs. As we observed, if we instantiate the 'Shark' class and call our 'Swim' method, the result should be the same message as the one inside the 'MarineAnimal' class. Similarly, if we instantiate the 'Whale' class and call our 'Swim' method, we should see the message inside the 'Swim' method, which we replaced using the @override annotation. When we run the main function, the result is as follows:


Abstract Class:

Now, let's explore an example using the concept of Abstract Classes that we discussed earlier. As we've learned, abstract classes can contain abstract methods. What does that mean? In the previous example, when you examine the 'MarineAnimal' class, it has a 'Swim' method with a concrete implementation, right? This is because in regular classes, when we define a method, we can't leave it with an empty body; we need to specify what that method should do. However, with abstract classes, we can have empty method bodies, leaving only the method annotation inside the abstract class. Let's look at an example to clarify this.

In the example above, we can see how abstract classes work. We often use the interface name in abstract classes, a practice known as Explicit Interface. Notice that inside the abstract class 'MarineAnimalInterface,' we only have the annotation of the 'Swim' method. (In this case, the 'Swim' method doesn't have any parameters, but we can have methods with parameters without any issues.) Now, even though we use the 'extends' keyword, we are still required to provide a concrete implementation of the 'Swim' method from the interface that we are using. Some key features of Abstract Classes are:

  • A class containing an abstract method must be declared as abstract, while a class declared as abstract may or may not have abstract methods; it can have either abstract or concrete methods.
  • An abstract class cannot be instantiated.
  • An abstract class can be extended, but if you inherit from an abstract class, you must ensure that all the abstract methods within it are implemented.

In conclusion, we've delved into the world of Dart's 'implements' keyword and abstract classes, exploring how they facilitate structured and consistent development in object-oriented programming. We've seen how 'implements' enforces a contract, ensuring that classes adhere to specific interfaces, and how abstract classes provide a foundation for both abstract and concrete methods. These concepts are fundamental for building robust, maintainable software in Dart.

In our next topic, we will shift our focus to the 'with' keyword in Dart, an essential feature that enables mixin-based inheritance. Stay tuned as we explore how 'with' enhances code reusability and modularity, further expanding our Dart programming capabilities.




Princewill Inyang

Software Developer | Backend Developer | Python | Nodejs | Technical Writer

7 个月

Thanks for sharing.

回复

要查看或添加评论,请登录

Matheus Felipe的更多文章

社区洞察

其他会员也浏览了