
Concrete Classes in Dart
30 March, 2023
3
3
1
Contributors
Concrete Classes
In the Dart programming language, a concrete class is a regular class that can be instantiated and has its own state and behavior. It can define constructors and methods, and can be subclassed using inheritance.
Syntax
Here is the syntax for defining a concrete class in Dart:
class ClassName {
// class members (fields, methods, etc.)
}
A class can have fields to store data, and methods to define its behavior. It can also define constructors to initialize its fields when an object is created.
Here is an example of a simple concrete class in Dart:
class Car {
String make;
String model;
int year;
Car(this.make, this.model, this.year);
void startEngine() {
print("Engine started");
}
}
Constructors
In Dart, a constructor is a special method that is used to create and initialize objects of a class. A constructor has the same name as the class and can be used to set initial values for the class's properties or perform other initialization tasks.
There are two types of constructors in Dart: default constructors and named constructors.
Default Constructors
A default constructor is created automatically by Dart when one is not explicitly defined. The default constructor has no parameters and simply initializes all instance variables to their default values.
Here's an example of a default constructor in Dart:
class Person {
String name;
int age;
Person() {
name = "John Doe";
age = 30;
}
}
In this example, the Person class has a default constructor that initializes the name property to "John Doe" and the age property to 30.
Named Constructors
A named constructor is a constructor that has a name other than the class name. Named constructors are useful when a class has multiple ways to create objects with different initialization parameters.
Here's an example of a named constructor in Dart:
class Person {
String name;
int age;
Person(this.name, this.age);
Person.fromName(String name) {
this.name = name;
this.age = 30;
}
}
In this example, the Person class has a named constructor "Person.fromName" that takes a "name" parameter and sets the name property to the parameter value and the age property to 30.
Constructor Chaining
Constructor chaining is a technique that allows one constructor to call another constructor in the same class. Constructor chaining is useful when a class has multiple constructors with similar initialization tasks.
Here's an example of constructor chaining in Dart:
class Person {
String name;
int age;
Person(this.name, this.age);
Person.defaultValues() : this("John Doe", 30);
}
In this example, the Person class has a constructor "Person.defaultValues" that calls the default constructor using the ":" syntax. The default constructor initializes the name property to "John Doe" and the age property to 30.
In summary, constructors in Dart are special methods that are used to create and initialize objects of a class. Dart supports two types of constructors: default constructors and named constructors. Constructor chaining is a technique that allows one constructor to call another constructor in the same class, making initialization tasks more efficient and convenient.