Dart supports object oriented programming which Flutter framework relies on. Classes have 2 main concepts: attributes and methods. Attributes are needed to store various data in the instance of Class and methods are functions, which can use Class attributes for various manipulations. To understand OOP and its difference from functional programming visit [this website](https://www.freecodecamp.org/news/object-oriented-programming-concepts-21bb035f7260/).
Dart supports object oriented programming, and it features heavily in Flutter. Classes have 2 main concepts.
- Attributes: store data about the instance of a class.
- Methods: are func functions, which can use class attributes for various manipulations.
Here is an example of the Class in Dart:
Here is an example of a class in Dart. `Point` is the name of the class, while `x` and `y` are attributes:
```dart
class Point {
@ -33,69 +35,65 @@ class Point {
}
```
**Point** - name of the class, **x, y** - are attributes.
What if you want to initiate a Point with different x and y? To do that declare a **_Constructor_** - which is a function that specifies how to create object of a class given a set of parameters.
What if you want to initiate a `Point` with different `x` and `y`? We use constructors to solve this problem. A constructor is a function which specifies how to instantiate an instance of a class, from a set of given parameters.
```dart
class Point {
double x = 0; // attribute
double y = 0; // attribute
// Attributes
double x = 0;
double y = 0;
// Constructor
Point(double x, double y) {
this.x = x; // initializing attributes
// Initializing attributes
this.y = y;
this.x = x;
}
}
```
In Dart we can also use Constructor declaration of type **_Constructor(param1, param2...);_** thus saving a few lines of code if all we want to do is to pass arguments to values.
In Dart we can also use **constructor declaration** to save a few lines of code.
```dart
class Point {
double x = 0;
double y = 0;
double x;
double y;
Point(this.x, this.y); // "this" is meant for current class.
Point(this.x, this.y);
}
```
Now let's instantiate two objects of class Point:
Now let's instantiate two objects of class `Point`:
```dart
var p1 = Point(5, 4);
var p2 = Point(8, 3);
```
What if we would like to know what is the distance between these 2 points? We can do that by declaring a class function (or method of the class) and passing one of the objects as a parameter:
Suppose we want to know the distance between these 2 points? We can do that by declaring a method, and passing one of the objects as a parameter: