In this section, we will delve into the core concepts of Object-Oriented Programming (OOP) in Dart: classes and objects. Understanding these concepts is crucial for building robust and scalable applications.
Key Concepts
- Class: A blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data.
- Object: An instance of a class. It is created using the class blueprint and can have its own state and behavior.
Creating a Class
A class in Dart is defined using the class
keyword followed by the class name. Here is a simple example:
class Person { // Properties String name; int age; // Constructor Person(this.name, this.age); // Method void introduce() { print('Hi, my name is $name and I am $age years old.'); } }
Explanation
- Properties: Variables that hold the state of the class. In this example,
name
andage
are properties. - Constructor: A special method that is called when an object is instantiated. The constructor in this example initializes the
name
andage
properties. - Method: A function defined within a class that can operate on the class's properties. The
introduce
method prints a message using the class properties.
Creating an Object
To create an object of the Person
class, you use the new
keyword (optional in Dart) followed by the class name and the required parameters for the constructor:
void main() { // Creating an object of the Person class Person person1 = Person('Alice', 30); // Calling a method on the object person1.introduce(); }
Explanation
- Instantiation:
Person person1 = Person('Alice', 30);
creates a new object of thePerson
class with the name 'Alice' and age 30. - Method Call:
person1.introduce();
calls theintroduce
method on theperson1
object, which prints the introduction message.
Practical Exercise
Exercise 1: Define a Class and Create Objects
- Define a class
Car
with the following properties:make
(String)model
(String)year
(int)
- Add a constructor to initialize these properties.
- Add a method
displayInfo
that prints the car's details. - Create two objects of the
Car
class and call thedisplayInfo
method on each.
Solution
class Car { // Properties String make; String model; int year; // Constructor Car(this.make, this.model, this.year); // Method void displayInfo() { print('Car: $make $model, Year: $year'); } } void main() { // Creating objects of the Car class Car car1 = Car('Toyota', 'Corolla', 2020); Car car2 = Car('Honda', 'Civic', 2018); // Calling methods on the objects car1.displayInfo(); car2.displayInfo(); }
Explanation
- The
Car
class is defined with propertiesmake
,model
, andyear
. - The constructor initializes these properties.
- The
displayInfo
method prints the car's details. - Two objects,
car1
andcar2
, are created and their details are printed using thedisplayInfo
method.
Common Mistakes and Tips
- Uninitialized Properties: Ensure all properties are initialized either directly or through the constructor.
- Method Calls: Remember to use the object name followed by a dot to call methods on an object.
- Naming Conventions: Use meaningful names for classes, properties, and methods to make your code more readable.
Conclusion
In this section, we covered the basics of classes and objects in Dart. You learned how to define a class, create objects, and use methods to interact with the object's properties. These concepts form the foundation of Object-Oriented Programming and are essential for building complex applications.
Next, we will explore constructors in more detail, including different types of constructors and their uses.
Dart Programming Course
Module 1: Introduction to Dart
- Introduction to Dart
- Setting Up the Development Environment
- Your First Dart Program
- Basic Syntax and Structure