Menu
Coddy logo textTech

Introduction to OOP

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 3 of 110.

Object-Oriented Programming (OOP) organizes code around objects that contain data (instance variables) and functions (methods).

Create a file called car.dart with a class

class Car {
  // empty class body
}

Create another file called driver.dart to use the class

import 'car.dart';

Create an object from the Car class using the class name followed by parentheses

void main() {
  Car myCar = Car();
}

Check the type of your object using runtimeType

  print(myCar.runtimeType);

Output:

Car

This confirms you've successfully created an object from your Car class. In OOP, a class is like a blueprint, and an object is what you build from that blueprint. In Dart, you create objects by calling the class name like a function: Car().

challenge icon

Challenge

Medium

In this challenge, you'll use a Car class defined in car.dart and create an object from it in driver.dart.

Update driver.dart to:

  1. Create an object from the Car class using Car() and store it in a variable called myCar

The driver file will print the runtime type to verify your implementation.

Cheat sheet

Object-Oriented Programming (OOP) organizes code around objects that contain data (instance variables) and functions (methods).

A class is like a blueprint, and an object is what you build from that blueprint.

Define a class:

class Car {
  // empty class body
}

Import a class from another file:

import 'car.dart';

Create an object from a class using the class name followed by parentheses:

Car myCar = Car();

Check the type of an object using runtimeType:

print(myCar.runtimeType); // Output: Car

Try it yourself

import 'car.dart';

void main() {
  // TODO: Create a Car object named myCar
  Car myCar = ?;

  print(myCar.runtimeType);
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming