How do I create and use classes and objects in Dart?

 



In Dart, creating and using classes and objects follows a similar structure to other object-oriented languages. Here’s a basic overview:

1. Creating a Class

A class is a blueprint for creating objects. It defines properties (variables) and methods (functions) that the objects created from the class will have.

Here’s an example of a simple class:


class Person {

  

  String name;

  int age;

  Person(this.name, this.age);


  void displayInfo() {

    print('Name: $name, Age: $age');

  }

}


In this class:

  • name and age are properties (also called fields or attributes).

  • The constructor Person(this.name, this.age) initializes the properties when the object is created.

  • displayInfo is a method that displays the person's name and age.

2. Creating an Object

Once you’ve defined a class, you can create objects from it. Objects are instances of the class and have access to the class’s properties and methods.


void main() {


  Person person1 = Person('John Doe', 25);

  print(person1.name); 

  person1.displayInfo(); 

}


3. Private Properties and Methods

In Dart, you can make properties and methods private by prefixing them with an underscore (_).


class Car {


  String _model;


  Car(this._model);

  String getModel() {

    return _model;

  }

}


4. Getters and Setters

Getters and setters allow controlled access to class properties. Dart supports automatic getters and setters, but you can also define them explicitly.


class Rectangle {

  double _width;

  double _height;


  Rectangle(this._width, this._height);


  double get area => _width * _height;


  set width(double value) {

    _width = value;

  }


  set height(double value) {

    _height = value;

  }

}


5. Inheritance

In Dart, a class can inherit properties and methods from another class using the extends keyword.

dart

class Animal {

  void makeSound() {

    print('Animal makes a sound');

  }

}


class Dog extends Animal {

  @override

  void makeSound() {

    print('Dog barks');

  }

}


void main() {

  Dog dog = Dog();

  dog.makeSound(); // Output: Dog barks

}


6. Abstract Classes and Interfaces

An abstract class is a class that cannot be instantiated. You use it as a base class that other classes inherit from. It can contain abstract methods (methods without implementation).

abstract class Shape {

  double getArea();

}


class Circle extends Shape {

  double radius;


  Circle(this.radius);


  @override

  double getArea() => 3.14 * radius * radius;

}


void main() {

  Circle circle = Circle(5);

  print(circle.getArea()); // Output: 78.5

}


Summary

  • Class: A blueprint for creating objects.

  • Object: An instance of a class.

  • Constructor: A special method to initialize objects.

  • Methods: Functions inside a class.

  • Getters and Setters: Control how you access and set property values.

  • Inheritance: Reusing and extending functionality from another class.

  • Abstract Classes: Classes with unimplemented methods, serving as a template for subclasses.

You can use these building blocks to create more complex applications in Dart.



Hire Me: https://www.fiverr.com/s/Gzyjzoz

Facebook Profile: https://www.facebook.com/profile.php?id=61566171451193

---------------------

Medium Profile :https://medium.com/@softcode9x/how-do-i-create-and-use-classes-and-objects-in-dart-2b2a2e39d422

---------------------

Linkedin Profile: https://www.linkedin.com/company/softcode9x/?viewAsMember=true

------------------

#FlutterApp #FlutterUi #Flutter #FlutterDeveloper #FlutterFirbase #Amanda #Paula #SusmoyDutta #infoosp #softcode9x #SoftCode9X #SoftCodeNinex #NineSwiftCode #SoftCodeQuick9 #9XSoftPro #NineSpeedCode #FastCode9X #NineXSoft #SwiftSoft9X #9XVelocityCode #TurboSoft9 #RapidCode9X #NineXAccel #SoftCodeRush9 #9XHyperCode #NineXExpress #SoftCode9Jet #QuickSoft9X #NineXBoost #SoftCode9Turbo #9XFlashCode #Innovation #Speed #Efficiency #Modern #Technology #Sleek #Dynamic #Precision #Digital #Simplicity #Futuristic #Connectivity #Code #Software #Minimalist #Agility #Performance #Scalability #Edge #Flow #Cutting-edge #Versatility #Innovation #Matrix #Circuit #Abstract #Cloud #Binary #Integration #Velocity #Quantum #Grid #Hex #Pixel #Network #Algorithm #Synergy #Revolution #Core #Nexus #bastSoftCode9X #techsoftCode9X #itSoftCode9X#dev #SoftCode9X #softwareSoftCode9X #infoSoftCode9X #tclSoftCode9X #flutterSoftCode9X #websitSoftCode9X #mobileSoftCode9X #softwaredevelpomentSoftCode9X #developSoftCode9X #softwareSusmoydutta #SoftwareDeveloperSusmoyDutta #infoosplife

Comments

Popular posts from this blog

At SoftCode9X, we’re thrilled to announce the successful completion of our latest project! ๐Ÿš€