Classes in Dart
Overview
Dart is a modern, open-source programming language developed by Google. It is designed to be versatile, efficient, and easy to learn. Originally released in 2011, Dart was created to enable developers to build fast, scalable, and high-quality web and mobile applications.
Dart is a versatile language that can be used for web and mobile app development, server-side scripting, and command-line utilities. With its focus on performance, productivity, and cross-platform capabilities through Flutter, Dart has gained popularity among developers and continues to evolve as a strong contender in the world of modern programming languages.
Introduction to Classes in Dart
Classes are a key idea in Dart that are used to builisbjects and specify their attributes. Classes in Dart are the cornerstone of the object-oriented programming (OOP) paradigm, and Dart is an OOP language. They offer a guide for building objects with certain properties and operations. Let's explore the specifics of classes in Dart:
Declaration of Class:
In Dart, you must use the class keyword together with the class name to declare a class. Curly braces are used to encapsulate the class body. An easy example of a class declaration is shown below:
Now that we know how to declare Classes in Dart let’s see its Instance Variables
Fields (Instance Variables)
A field is a declared variable inside a class that stores the state or data of any objects derived from it. The field values for each instance of an object are unique. Instance variables are another name for fields. Here is an illustration of a class that has fields:
The Student class in this illustration includes two fields: name and age. Additionally, the constructor of the Student class can initialize these variables.
Creating a class instance
Use the new keyword followed by the class name to create an instance of the class. The same syntax is shown below.
Syntax
The instantiation is carried out using the new keyword. The constructor is called by the expression's right-hand side. If the constructor is parameterized, values must be supplied to it.
Instantiating a class
Now that we are familiar with the Instance of a Class and how to declare Classes in Dart then let’s see constructors
Constructors
Constructors are special methods called constructors that are used to generate and initialize objects that belong to a class. The class and they have the same name. Dart allows for the use of several constructors, including name, default, and parameterized constructors.
In the preceding illustration, the name and age fields were initialized using a parameterized constructor. The constructor parameters are automatically assigned to the class fields using the shortcut Person(this. name, this. Age).
An exclusive class function called a constructor is in charge of initializing the class variables. The constructor for the class is also defined by Dart with the same name. Because a constructor is a function, it can take parameters. Constructors, on the other hand, are unable to have return types. A default no-argument constructor is offered to you if you don't define one.
Syntax
Example The example that follows demonstrates how to utilize constructors in Dart.
Output:
Named Constructors
To allow a class to declare numerous constructors, Dart offers named constructors. Given below is the syntax for named constructors.
Syntax:
Example The example that follows demonstrates how to utilize named constructors in Dart.
output
The this Keyword
The current instance of the class is referred to via the this keyword. Here, the name of the argument and the field for the class are the same. Consequently, the this keyword is prefixed to the class's field to prevent ambiguity. The same is clarified by the example that follows:
Example This keyword in Dart is used in the example below to demonstrate how to do it.
Output
Methods
Methods are functions that are specified inside of a class and allow its objects to carry out particular behaviors or actions. They serve as a representation of the actions of objects derived from the class. Here is an illustration of a class that has methods:
Code:
Explanation:
In this illustration, the sayHello() function of the Person class prints a greeting along with the person's name and age.
Getters and setters
Getters and setters are specialized methods that provide access to class fields and aid in regulating how the fields are read or updated.
Code:
Explanation:
In this example, we access the _width and _height fields using getters and setters, guaranteeing that their values are always positive.
OOPs Implementation
Classes in Dart provide a robust implementation of object-oriented principles like encapsulation, inheritance, and polymorphism. By leveraging these principles, you can write more modular, reusable, and maintainable code.
Inheritance
Inheritance is the principle that allows a class (subclass or derived class) to inherit properties and methods from another class (superclass or base class). This promotes code reuse and establishes an is-a relationship between the classes in dart.
In Dart, you can create a subclass that inherits from a superclass using the extends keyword. The subclass can override the superclass's methods to provide its specific implementation.
Code:
Explanation: In this illustration, the superclass is the Animal class, and the subclass is the Dog class. The name field and the makeSound() function are inherited by the Dog class from the Animal class.
Encapsulation
The concept of combining data (fields) and the methods (functions) that manipulate that data into a single unit, or class, is known as encapsulation. Outsiders are kept in the dark about the underlying implementation details, and getters and setters are often used to restrict access to the class's fields.
Encapsulation is possible in Dart by designating fields as private using the underscore (_) prefix. To prevent outside code from directly altering the class's internal state, private fields may only be accessed within the class in which they are specified. Getters and setters can be used to grant access to private fields.
Code:
Explanation:
In this example, the BankAccount class has a private field _balance. The deposit() and withdraw() methods allow controlled access to modify this field. External code can only access the _balance field through the balance getter, which ensures read-only access.
Conclusion
- Classes in Dart are a fundamental concept and object-oriented programming in general.
- They allow you to organize code, create reusable components, and model real-world entities effectively.
- By understanding how to declare classes in Dart, and define fields, methods, and constructors, you can build complex and maintainable applications in Dart.
- Additionally, you can leverage features like inheritance, interfaces, and abstract classes in Dart to create a well-structured and extensible codebase.