Day Six of the #60Days of Flutter Challenge
Learning OOP: finishing up by learning about classes, constructors, and class functions
Welcome to Day Six of the #60Days of Flutter challenge. Today, our last day of Mike Dane’s beginning Dart tutorial, we learned about some object-oriented programming (OOP) concepts — creating classes, constructors, and class functions.
Side note: In the tutorial, Mike said that the terms function and method could be used interchangeably. In an effort to not confuse myself, I’ve written function throughout this article.
The idea behind OOP is to structure your code to resemble “real-world” objects. Writing code as objects allows you to write once and reuse it as needed throughout your program. This leads to less code (smaller programs) that are easier to read.
Let’s say — for example — that you want to create a program that prints out a series of books with the above pieces of information. As we’ve learned in a previous lesson, we could make a simple print statement in the main() function. While this is no big deal for one book, what about 100? What if you want to add information? Is it worth doing 100 times?
To call a class inside the main function we use:
Class name function name = Classname();
In our example it would be:
Book hp = Book();
Now we can add the information for this particular book using the function name + the variables in the Book class.
ex. (adding the title) hp.title = “Sorcerer’s Stone”.
At its core, coding seems to be about creating an object and figuring out how you want to use it.
By writing a Book class, we’ve created a basic book object with three different parameters (variables). Inside of the main() function, we use the parameters to create specific instances (objects) of additional books.
Basically we have a single “vanilla” book (object). When we call it in the main() function we are adding the title, author, and number of pages for each specific book we define.
There is one small problem with what we’ve created above. Typing out the words title, author, and numPages for each instance will get cumbersome — and flies directly in the face of our “Don’t Repeat Yourself(DRY)” coding ideal.
To write once and reuse our code, we need to create a constructor function within the Book class.
A constructor constructs an object and gives it its initial values (parameters).
In our example, we name our constructor book (same as the class name) and define its parameters (same as the class variables) using the keyword this.
We can call our parameters anything we want (even this.title = title; works fine), but it’s easier (at least early in the learning process) to name them something different than the variable names.
The process at that point is almost the same as the way we called a class in the previous example. The difference is that a constructor requires each of the parameters to work correctly.
We can’t just put:
Book mr = Book(); anymore, we need to put in each of the necessary variable parameters called for by the constructor.
This is a fantastic tool to get comfortable using for a couple of reasons. One, it is less code to write. Notice that I’ve commented out the hp.title, hp.author, and hp.numPages? The same information is accomplished in one line of code with a constructor. Two, it’s much easier to read.
Constructors are your friend. Learn how to use them.
Finally, we looked at creating class functions.
Class functions work similarly to class constructors. An object created in the main() function can use both. The benefit to creating them over a constructor is that they don’t necessarily have to be called separately by every object. A class function isn’t a required parameter, just an opportunity to better define an object (when needed).
In the above example, we are determining which of the students listed is an honor student. Using the class function hasHonors() we can see that Matt — which a GPA of 3.5 — does. Because it is a boolean function, the program would answer true.
That’s it for Day Six. If you are interested in learning Dart, I highly recommend beginning with Mike Dane’s tutorial:
Mike makes things easy to understand and doesn’t bog down your learning with confusing terminologies — he just shows you how to write the code. I felt like I learned quite a bit in a short amount of time, and am confident enough in my understanding to keep going.
The feedback I’ve been given says that knowing Dart is a big part of learning Flutter, so I’ll continue to practice what I’ve learned while moving forward.
On to Day Seven (aka Day One of Flutter):
In case you missed it, here is the post for day five: