Day 13 of the Sixty Days of Flutter Challenge
Revisiting OOP: Resuming the journey by returning to classes and constructors
Welcome to Day Thirteen of the Sixty Days of Flutter Challenge. After a short hiatus, we are back to go over classes and constructors one last time before we dive headfirst into Flutter.
Dart is an object-oriented programming (OOP) language. In essence, this means that objects are represented through code. As we’ve learned previously, writing code as objects allows us to write once and reuse it as needed throughout our programs (remember D.R.Y.). This leads to less code and, therefore, smaller programs that are much easier to read.
At the top of the OOP chain is a super class. It is nothing more than a blueprint of an object. Don’t let its simple name fool you, though. It is the encapsulation of an object — containing fields, functions, constructors, and more as needed.
In the above example, we’ve created a class named Vehicle with two parameters (variables). According to our class, every vehicle in existence (according to our code) will have a model and a year.
Underneath the variable is a constructor.
A constructor has the same name as the class and is a function that doesn’t return any value. It is used to initialize an object when it is created.
In the below example, we have used the Vehicle class we created to initialize a vehicle object. The above Vehicle class is a simple blueprint of a vehicle. By creating an object, we are now making a specific version of a vehicle — a 2020 Tesla.
We can now use the vehicle object along with the parameters created by the Vehicle class.
What happens when we need access to more parameters than the super class provides? We can create an additional class to instantiate a more specific version of an object using the keyword extends.
In the above example, the Car class extends the Vehicle class. This means that it inherits the attributes of the Vehicle super class to use alongside its own.
We repeat the class name to create a constructor. However, this time, we add the super class parameters (Vehicle) and sub class (Car). Note that we have to define the type for variables from the super class, but not the sub class.
Even though we’ve added them in, a constructor cannot inherit the super class’s attributes on its own. We need to create a super constructor to accomplish that. To achieve that in the above example, we add :super(super class parameters) at the end of the constructor.
Okay — I know what you’re thinking. What if I want to go further? Can I create an entirely separate bicycle class that also extends the vehicle class?
Short answer: Yes.
Long answer: Yes, but each class can only have one extends clause. So you can’t have class Bicycle extends Car extends Vehicle.
However, you can convert a sub-class into a super class. Doing this will allow you to use the attributes from both.
In the above example, we’ve created a Bicycle class that extends the Car class, which extends the vehicle class. As you can see by the Bicycle constructor, we now have access to the model, year, price, and riders attributes in every bicycle object we create.
I agree. This stuff is awesome.
You can see all of this in action with the different objects we’ve created in the main() function.
The vehicle object (named vehicle), is a 2020 Tesla. When we print the model using vehicle.model (objectName.parameter), we are told it is a Tesla.
In the next object, car1 is a 1920 Ford with a price of 2000. We’ve printed the model (extended from the Vehicle class) and the price.
Last, but certainly not least, is the bike object. It is a 2000 Huffy with a price of 1500 that allows two riders (I know — strange variable choice, but it works for the example). Using the bike object, we can print the model (created in the Vehicle class and extended from the Car class), price (extended from the Car class), and the number of riders.
Using Classes and constructors is a big part of programming. It gives us a ton of flexibility to create clean, re-usable code, breaking our programs into bite-sized pieces without losing the flexibility to accomplish our end goal — whatever that may be.
That’s it for Day Thirteen. As I’ve mentioned previously (more than once), I am a big fan of Mike Dane’s Dart tutorial on YouTube. However, to get an additional perspective, I watched a second tutorial by Mahmud Ahsan and the good people at FreeCodeCamp. I came away equally impressed and highly recommend it.
In case you missed it, Here is Day Seven.
Day Seven of the #60Days of Flutter Challenge
Introducing Flutter, the world of widgets, and creating a first Flutter app
On to Day 14.