Dart: implementing Object Oriented Programming (OOP)
The object oriented programming provides several benefits for system design. It makes the code easier to maintain, easier to extends, and also implements several well-know design patterns.
Using the Dart
programing language classes can be simple defined utilizing the class keyword,
followed by the class name, and then delimitated the class body with the key
brackets {}, like in the example below, presenting one class with two methods.
class ProgrammingLanguage{
void compile(){
print("Compiling...");
}
void execute(){
print ("Executing...");
}
}
When we need to add new features to this class, it is possible to use the inherence concept, defining a new class that will receive all implementation this ProgrammingLanguage class already has done. And this inherence is done using the extends keywork, like in the example below, which the Dart class has the compile() and the execute() methos from its superclass (ProgrammingLanguage) but also has the code() method implemented by its own.
class Dart extends ProgrammingLanguage
void code(){
print("Coding...");
}
}
Another important
feature from Object Oriented Programming is the usage of abstract classes and
interfaces. In Dart programming language there is no explicit definition of
interface, they also are defined with abstract keyword using abstract classes.
However, a subclass can decide if it will extends the abstract class ( as
normal inherence) or implements that, as an interface. You can see in the
example below the usage of a abstract class as an interface.
abstract class DynamicTypingProgrammingLanguage{
void typeChangeHandle();
}
class Dart extends ProgrammingLanguage implements DynamicTypingProgrammingLanguage{
void code(){
print("Coding...");
}
@override
void typeChangeHandle() {
print ("handling dynamic types");
}
}
It is important to highlight that Dart does not support multiple class inherence, like another programming languages as JAVA, then and alternative for that is the usage of interfaces as presented in the last example.
You can
download the source code from here:
https://github.com/rafaelqg/code/blob/main/oop_dart.dart
You may see
a demonstration of this code in a video class here:
Comments
Post a Comment