Dart Classes and Objects

From Dart Wiki
Jump to navigation Jump to search

Dart Classes and Objects[edit]

Dart is an object-oriented programming language, which means it provides support for classes and objects. Classes are the blueprint or template for creating objects, while objects are instances of those classes. In this article, we will explore the concept of classes and objects in Dart.

Declaring a Class[edit]

To declare a class in Dart, use the `class` keyword followed by the class name. Here's an example:

``` class Person {

 String name;
 int age;

} ```

In the above code, we declare a class named `Person` with two properties: `name` of type `String`, and `age` of type `int`.

Creating Objects[edit]

To create an object of a class in Dart, use the `new` keyword followed by the class name and parentheses. Here's an example:

``` Person person = new Person(); ```

In the above code, we create a new object `person` of type `Person`.

Accessing Class Members[edit]

To access the properties and methods of a class, use the dot (`.`) operator. Here's an example:

```dart person.name = 'John Doe'; person.age = 30; print('Name: ${person.name}, Age: ${person.age}'); ```

In the above code, we set the `name` and `age` properties of the `person` object and then print them using string interpolation.

Constructors[edit]

Constructors are special methods used to create and initialize objects of a class. Dart provides a default constructor if you don't explicitly declare one. Here's an example of a custom constructor:

```dart class Person {

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

}

Person person = new Person('John Doe', 30); ```

In the above code, we declare a custom constructor `Person` which takes `name` and `age` as parameters and assigns them to the corresponding properties.

Methods[edit]

Methods are functions defined inside a class. They can manipulate the data of an object or perform other operations. Here's an example:

```dart class Person {

 String name;
 int age;
 
 Person(this.name, this.age);
 
 void sayHello() {
   print('Hello, my name is $name.');
 }

}

Person person = new Person('John Doe', 30); person.sayHello(); // Output: Hello, my name is John Doe. ```

In the above code, we define a method `sayHello` which prints a greeting using the `name` property of the object.

Inheritance[edit]

Dart supports inheritance, allowing a class to inherit properties and methods from another class. Here's an example:

```dart class Student extends Person {

 String university;
 
 Student(String name, int age, this.university) : super(name, age);

} ```

In the above code, we define a `Student` class which inherits from the `Person` class. It adds an additional property `university`.

Conclusion[edit]

In this article, we explored the concept of classes and objects in Dart. We learned how to declare a class, create objects, access class members, use constructors, define methods, and implement inheritance. Understanding classes and objects is essential for developing object-oriented applications in Dart.

See Also[edit]