Classes
In Dart, classes are a blueprint for creating objects. They are an integral part of object-oriented programming (OOP). They define the methods, properties, and behavior of objects. For example, a class named Phone
may have properties like .color
and .brand
as well as methods like .call()
and .text()
.
Syntax
class ClassName {
// Class body
// Properties
type propertyName;
// Methods
returnType methodName() {
// Method body
}
}
class
: The keyword used to create a class.ClassName
: The name of the class. It follows the UpperCamelCase convention.
Class Instances
In Dart, an object is an instance of a class that consists of properties and methods. It can only be created after creating a class.
A new object of a particular class can be created using the following syntax:
ClassName objectName = ClassName();
Example
The following example demonstrates how to create an object in Dart:
class House {// Defining propertiesString? color;int? numberOfRooms;// Defining a methodvoid houseInfo() {print("House color: $color");print("Number of rooms: $numberOfRooms");}}void main() {// Creating an object of the `House` classHouse house = House();house.color = "White";house.numberOfRooms = 5;house.houseInfo();}
The output for the above example is as follows:
House color: WhiteNumber of rooms: 5
Abstract Classes
In Dart, a class can be declared as an abstract class using the abstract
keyword. If a class is declared abstract, new objects cannot be instantiated from that class. The purpose of an abstract class is to allow other classes to inherit from it.
A class can be declared abstract using the following syntax:
abstract class ClassName {
// Class body
...
}
Abstract Methods
In Dart, an abstract method is a method declared without any implementation details. Instead of providing a method body, an abstract method is defined only by its signature, followed by a semicolon (;
). Therefore, subclasses must provide the implementation details for abstract methods when they inherit from an abstract class.
Example
Following is an example that demonstrates the usage of an abstract method:
abstract class Pet {// Defining an abstract methodvoid feed();}class Dog extends Pet {@overridevoid feed() {print('Feeding dog...');}}class Cat extends Pet {@overridevoid feed() {print('Feeding cat...');}}void main() {Dog dog = Dog();dog.feed();Cat cat = Cat();cat.feed();}
The above code will produce the following output:
Feeding dog...Feeding cat...
Mixin Classes
In Dart, mixins
are classes intended to be used for reusable methods that other classes can use.
Syntax for Mixin Class
mixin MixinName {
// Mixin body
...
}
class ClassName with MixinName {
// Class body
...
}
Example
Following is an example that demonstrates the usage of a mixin class:
// Define mixins for specific behaviorsmixin Swim {void swim() {print('Swim');}}mixin Fly {void fly() {print('Fly');}}mixin Walk {void walk() {print('Walk');}}// Define classes that use these mixins// The Duck class can swim, fly, and walkclass Duck with Swim, Fly, Walk {void display() {print('Ducks can:');}}// The Human class can swim and walk but cannot flyclass Human with Swim, Walk {void display() {print('Humans can:');}}void main() {// Create a Duck instanceDuck duck = Duck();duck.display();duck.swim();duck.fly();duck.walk();// Create a Human instanceHuman human = Human();human.display();human.swim();human.walk();}
The above code will produce the following output:
Ducks can:SwimFlyWalkHumans can:SwimWalk
All contributors
- Anonymous contributor
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.