Fluttering Dart - OOP. Classes, Objects, Interfaces, and A Lot - by Constantin Stan - Level Up Coding
Fluttering Dart - OOP. Classes, Objects, Interfaces, and A Lot - by Constantin Stan - Level Up Coding
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one
Search
Constantin Stan
Dec 8, 2019 · 6 min read
FLUTTERING DART
Constantin Stan
Fluttering Dart: OOP 429 Followers
Related
Flutter projects can use both platform-specific and cross-platform code. The
How To Make Yours Rock SOLID
latter is written in Dart, and, for building Flutter apps, some basic knowledge of SOLID Principles in Flutter/Dart
Dart is required.
Fluttering Dart’s goal is to explore fundamental knowledge and unveil tips &
tricks of the powerful programming language that brings Flutter to life. Help Status Writers Blog Careers Privacy Terms About
Knowable
307
In the previous parts of the series, we went through the Dart built-in data types,
functions, operators and control flow statements.
Some of the code examples can be tried out, and played with, using DartPad.
A class is a user-defined data type and in the examples, up to this point we’ve
already defined some classes, the most memorable probably being the Cat class.
In Dart, every object is an instance of a class and all classes descend from Object.
Dart also has a mixin-based inheritance and that comes to aid the lack of
multiple inheritances. This inheritance type allows the reuse of multiple class
bodies and the existence of exactly one superclass.
class Cat {
DateTime birthday;
// default
// it's here
// even if
// you can't see it
}
class Cat {
DateTime birthday;
// named
Cat.baby() {
birthday = DateTime.now();
}
}
class Cat {
DateTime birthday;
// main cosntructor
Cat(this.birthday);
constant — use when we need objects that never change; when calling such
constructors we should use the keyword const otherwise we won’t create
constants;
class CatTreat {
static final CatTreat catTreat = const CatTreat(1);
// constant
const CatTreat(this.quantity);
}
import 'dart:math';
The Pet factory constructor from above returns a random Cat or a Dog
(subclasses of Pet).
Callable classes
Dart classes can also behave like functions (they can be invoked, take arguments
and return something).
To enable this, we have to define the call() method inside the class.
class Cat {
DateTime birthday;
Cat(this.birthday);
String call() {
print('Meow!');
}
}
void main() {
var cat = Cat(DateTime.now());
cat();
// prints
// Meow!
}
Generators
Generators are used when we need to lazily produce a sequence of values. Dart
supports two types of generator functions:
Variables
There are two flavors: instance and class variables.
All uninitialized variables have by default the value null . Also, all of the
variables that are not final will generate an implicit getter and setter. The final
ones, will not generate a setter.
To create a class variable we’ll use the static keyword. These are useful for
class-wide state and constants. They are not initialized until they’re used.
Methods
Methods are functions that provide behavior for an object.
Like in the case of variables, here are also two flavors: instance and class
methods.
Static methods (class methods) do not operate on an instance, and thus do not
have access to this . They are best used as compile-time constants (for example,
Encapsulation
Dart doesn’t contain keywords for restricting access, like public , protected or
private used in Java. The encapsulation happens at library level, not at class
level.
There is a simple rule: any identifier (class, class member, top-level function, or
variable) that starts with an underscore _ it is private to its library.
Mixins are a way of reusing a class’s code in multiple class hierarchies. To use a
mixin, use the with keyword followed by one or more mixin names. To specify
that only certain types can use the mixin — for example, so your mixin can
invoke a method that it doesn’t define — use on to specify the required
superclass.
Abstraction
Abstraction is the process through which we define a class and its essential
characteristics, leaving implementation for its subclasses.
To declare an abstract class, we use the abstract keyword. These classes can’t be
instantiated and are useful for defining interfaces. Abstract classes can have
abstract methods.
There is no interface keyword. The way it works is that every declared class
defines an implicit interface containing all instance members of a class and of
any interfaces it implements. This means that any class can be implemented by
others without extending it.
A class can implement one or more interfaces by using the implements keyword.
Polymorphism
Polymorphism is achieved through inheritance and represents the ability of an
object to copy the behavior of another (the int or double are also a num).
We can use the extends to create a subclass and super to refer to the
superclass.
Subclasses usually override instance methods, getters, and setters. We can use
the @override annotation to indicate that we’re overriding a member.
Dart doesn’t allow overloading. To overcome this we can use the flexible
argument definitions (optional and positional).
Overall, Dart provides all of the bells and whistles that we need to use the OOP
paradigm.
In the next part of the Fluttering Dart series, we’ll delve into Futures and
Isolates to discover how to overcome Dart’s single-thread downside.
Tha(nk|t’)s all!
The Need For A New Markdown Parser and Terraform a Cloudflare domain for an
Why ephemeral IP of a GCE instance
If you were to ask any developer which…
PHP 8 First Release Candidate Performance Crust Network and Functionland partnering
Let’s follow up on the speed of the first… up on Web3 Developer Tools
DTM Plans for a Sunset Get Started with Git Command Line
Last November (2017), we released Launch t…