Lesson 1
Understanding Dart Classes in Object-Oriented Programming
Topic Overview

Hello and welcome to another essential course in your learning journey! Today, we'll delve into a core concept of Object-Oriented Programming (OOP): Dart classes. These classes act as templates that enable us to create objects—instances of these classes—each with its unique attributes and behaviors. The goal of this lesson is to understand what classes are, how to create them, and their significance in Dart programming.

Understanding Classes in Dart

A Dart class can be regarded as a construction blueprint. By using this blueprint, we can create objects with specific structures, each holding unique variable values. It's a convention in Dart that class names start with a capital letter to differentiate them from other identifiers.

Dart
1class Fruit { 2}

Here, Fruit is our blueprint or class. This blueprint allows us to generate multiple fruit objects, each with unique characteristics, similar to the construction of a building.

Creating an Instance of a Class

Creating an instance of a class—essentially bringing an object to life from a blueprint—involves using the new keyword:

Dart
1var apple = new Fruit();

In this case, apple is a specific instance or object of our Fruit class, just like a single building erected from a common blueprint.

Incorporating Methods Inside a Class

To bring functionality to our class, we incorporate methods. Methods are essentially functions that reside within a class. For our Fruit class, we'll include two methods: a printColor method and a printMessage method:

Dart
1class Fruit { 2 void printColor() { 3 print('Red'); // This prints out 'Red' when the method is called. 4 } 5 6 void printMessage(String name) { 7 print('Do you want a fruit, $name?'); // This prints a custom message when called. 8 } 9}

In this snippet, the printColor method simply prints Red to the console, showcasing a static behavior. Meanwhile, the printMessage method demonstrates dynamic behavior by accepting a String parameter (name) and incorporating it into the message printed to the console.

Using an Instance to Call Methods

After creating an instance of our class, it's time to utilize it to call the methods we have defined:

Dart
1var apple = new Fruit(); 2apple.printColor(); // Outputs: Red 3apple.printMessage('John'); // Outputs: Do you want a fruit, John?

In this example, we first create an instance of the Fruit class named apple. We then proceed to call both printColor and printMessage on our apple instance. As expected, the printColor method outputs Red, and the printMessage method utilizes the passed argument to generate a personalized output on the console.

Lesson Summary and Practice

Congratulations! You've now delved into Dart classes, uncovering their pivotal role within Object-Oriented Programming. Throughout this lesson, we've explored class creation, instance instantiation, and method invocation with both static and dynamic behaviors. Armed with these insights, you're poised for the practice sessions ahead. Remember, mastering new skills demands consistent effort and application. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.