Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart OOP


Share

Dart OOP

OOP stands for Object-Oriented Programming.

Procedural programming is about writing functions that work on data.

Object-oriented programming is about creating objects that contain both data and functions.

Dart is an object-oriented language.

Every value in Dart is an object, even numbers, strings, lists and functions.

This is why values have properties and methods that you can use with a dot .:

Example

void main() {
  print(7.isEven);
  print('dart'.toUpperCase());
  print([3, 1, 2].length);
}
Try it Yourself »

Result:

false
DART
3

Why Use OOP?

Object-oriented programming has several advantages:

  • It gives your programs a clear structure.
  • It keeps the code DRY, which stands for "Don't Repeat Yourself".
  • It makes the code easier to change, fix and test.
  • It lets you create reusable parts with less code.


Classes and Objects

Classes and objects are the two main parts of OOP.

A class is a template, or a blueprint, for objects.

An object is one thing that is created from a class.

Look at the table below to see the difference:

ClassObjects
FruitApple, Banana, Mango
CarVolvo, Ford, Toyota
PersonLiam, Emma, Noah

A class describes what every object of that kind has.

For example, every car has a brand and a year, and every car can drive.

The data of an object is stored in fields, and the functions of an object are called methods.

When you create an object, it gets all the fields and methods of its class, but with its own values.


A First Example

Here is a small class called Car, with two fields and one method.

We use it to create two car objects:

Example

class Car {
  String brand;
  int year;

  Car(this.brand, this.year);

  void drive() {
    print('The $brand from $year is driving');
  }
}

void main() {
  var car1 = Car('Volvo', 2020);
  var car2 = Car('Ford', 1969);

  car1.drive();
  car2.drive();
}
Try it Yourself »

Result:

The Volvo from 2020 is driving
The Ford from 1969 is driving

Both objects come from the same class, but each object has its own brand and year.

You will learn how this code works, step by step, in the next chapters.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.