DEV Community

SOFTCODE9X
SOFTCODE9X

Posted on

How do I create and use classes and objects in Dart?

Image description
In Dart, creating and using classes and objects follows a similar structure to other object-oriented languages. Here’s a basic overview:

  1. Creating a Class A class is a blueprint for creating objects. It defines properties (variables) and methods (functions) that the objects created from the class will have. Here’s an example of a simple class:

class Person {

String name;
int age;
Person(this.name, this.age);

void displayInfo() {
print('Name: $name, Age: $age');
}
}

In this class:
name and age are properties (also called fields or attributes).
The constructor Person(this.name, this.age) initializes the properties when the object is created.
displayInfo is a method that displays the person's name and age.

  1. Creating an Object Once you’ve defined a class, you can create objects from it. Objects are instances of the class and have access to the class’s properties and methods.

void main() {

Person person1 = Person('John Doe', 25);
print(person1.name);
person1.displayInfo();
}

  1. Private Properties and Methods In Dart, you can make properties and methods private by prefixing them with an underscore (_).

class Car {

String _model;

Car(this._model);
String getModel() {
return _model;
}
}

  1. Getters and Setters Getters and setters allow controlled access to class properties. Dart supports automatic getters and setters, but you can also define them explicitly.

class Rectangle {
double _width;
double _height;

Rectangle(this._width, this._height);

double get area => _width * _height;

set width(double value) {
_width = value;
}

set height(double value) {
_height = value;
}
}

  1. Inheritance In Dart, a class can inherit properties and methods from another class using the extends keyword. dart class Animal { void makeSound() { print('Animal makes a sound'); } }

class Dog extends Animal {
@override
void makeSound() {
print('Dog barks');
}
}

void main() {
Dog dog = Dog();
dog.makeSound(); // Output: Dog barks
}

  1. Abstract Classes and Interfaces An abstract class is a class that cannot be instantiated. You use it as a base class that other classes inherit from. It can contain abstract methods (methods without implementation). abstract class Shape { double getArea(); }

class Circle extends Shape {
double radius;

Circle(this.radius);

@override
double getArea() => 3.14 * radius * radius;
}

void main() {
Circle circle = Circle(5);
print(circle.getArea()); // Output: 78.5
}

Summary
Class: A blueprint for creating objects.
Object: An instance of a class.
Constructor: A special method to initialize objects.
Methods: Functions inside a class.
Getters and Setters: Control how you access and set property values.
Inheritance: Reusing and extending functionality from another class.
Abstract Classes: Classes with unimplemented methods, serving as a template for subclasses.
You can use these building blocks to create more complex applications in Dart.

Hire Me: https://www.fiverr.com/s/Gzyjzoz

Facebook Profile: https://www.facebook.com/iamsusmoydutta

Medium Profile :https://medium.com/@softcode9x/how-do-i-create-and-use-classes-and-objects-in-dart-2b2a2e39d422

Behance Profile: https://www.behance.net/susmoydutta

Linkedin Profile: https://www.linkedin.com/in/susmoy-dutta/

Top comments (0)