Object Oriented Programming in Dart: An Extensive Guide!!

Object Oriented Programming in Dart: An Extensive Guide!!

DART PROGRAMMING

Object-oriented programming (OOP) is a programming approach that organizes code around objects, representing real-world entities. It involves encapsulating data and behavior within objects, using classes as blueprints. OOP emphasizes principles such as encapsulation, inheritance, polymorphism, abstraction, and modularity to promote code organization, reusability, and flexibility.

Here's an extensive guide to object-oriented programming (OOP) in Dart:

  1. Classes and Objects:

    • Classes in Dart define the blueprint for creating objects. You can create classes using the class keyword.

    • Objects are instances of classes, and you can create objects using the new keyword or by omitting it in newer Dart versions.

    class Person {
      String name; //variables of a class
      int age; //variables of a class

      void sayHello() {
        print("Hello, my name is $name. I'm $age years old.");
      }
    }

    void main() {
      // Creating an object of the Person class
      var person = Person();
      person.name = "John";
      person.age = 30;

      // Calling the sayHello() method
      person.sayHello(); // Output: Hello, my name is John. I'm 30 years old.
    }
  • Properties represent the state or data associated with an object. They are declared using variables inside a class.

  • Methods define the behavior of an object. They are functions defined within a class and can manipulate the object's properties.

  1. Constructors:
  • Constructors are special methods used for object initialization. Dart provides default constructors if none is defined explicitly.

  • You can create named constructors to provide multiple ways to create objects.

  • The this keyword refers to the current instance of the class within a constructor.

  • There is another type of constructor known as redirecting constructor in Dart.

class Point {
  double x;
  double y;

  // Default constructor
  Point(this.x, this.y);

  // Named constructor
  Point.origin() {
    x = 0;
    y = 0;
  }

  // redirecting Constructors
  Point.addval(double x, double y) : this(x,y);
}

void main() {
  var p1 = Point(2, 3); // Using the default constructor
  print("Point p1: (${p1.x}, ${p1.y})"); // Output: Point p1: (2.0, 3.0)

  var p2 = Point.origin(); // Using the named constructor
  print("Point p2: (${p2.x}, ${p2.y})"); // Output: Point p2: (0.0, 0.0)

  var p3 = Point.addval(4,5); // Using the redirecting constructor
  print("Point p3: (${p3.x}, ${p3.y})"); // Output: Point p3: (4.0, 5.0)
}

Important Concepts of OOP in Dart:

  1. Inheritance:

    • Inheritance allows you to create new classes based on existing ones. The new class inherits the properties and methods of the parent class.

    • Dart supports single inheritance, meaning a class can only inherit from one superclass.

    • The extends keyword is used to establish inheritance relationships.

    class Animal {
      void eat() {
        print("Animal is eating.");
      }
    }

    class Dog extends Animal {
      void bark() {
        print("Dog is barking.");
      }
    }

    void main() {
      var dog = Dog();
      dog.eat(); // Output: Animal is eating.
      dog.bark(); // Output: Dog is barking.
    }
  1. Polymorphism:

    • Polymorphism allows objects of different classes to be treated as objects of a common superclass.

    • Dart achieves polymorphism through method overriding. You can override methods in a subclass to provide different implementations.

    class Animal {
      void makeSound() {
        print("Animal is making a sound.");
      }
    }

    class Dog extends Animal {
      @override
      void makeSound() {
        print("Dog is barking.");
      }
    }

    class Cat extends Animal {
      @override
      void makeSound() {
        print("Cat is meowing.");
      }
    }

    void main() {
      var dog = Dog();
      var cat = Cat();

      dog.makeSound(); // Output: Dog is barking.
      cat.makeSound(); // Output: Cat is meowing.

      // Polymorphism in action
      Animal animal = dog;
      animal.makeSound(); // Output: Dog is barking.

      animal = cat;
      animal.makeSound(); // Output: Cat is meowing.
    }
  1. Encapsulation:

    • Encapsulation refers to the practice of hiding internal implementation details and exposing only necessary information.

    • In Dart, you can mark properties and methods as private using the underscore (_) prefix.

    • You can provide getter and setter methods to control access to private properties.

  2. Abstraction:

    • Abstraction focuses on defining interfaces or abstract classes that provide a common set of methods that subclasses must implement.

    • Dart supports abstract classes and methods using the abstract keyword.

    • Abstract classes cannot be instantiated directly; they serve as a base for subclasses.

    abstract class Animal {
      void makeSound();
    }

    class Dog extends Animal {
      @override
      void makeSound() {
        print("Dog is barking.");
      }
    }

    class Cat extends Animal {
      @override
      void makeSound() {
        print("Cat is meowing.");
      }
    }

    void main() {
      Animal dog = Dog();
      dog.makeSound(); // Output: Dog is barking.

      Animal cat = Cat();
      cat.makeSound(); // Output: Cat is meowing.
    }
  1. Interfaces:

    • Dart does not have a separate interface keyword, as every class defines an interface.

    • You can implement multiple interfaces in a class using the implements keyword.

    • Classes implementing an interface must provide implementations for all the interface's methods.

  2. Static Members:

    • Static members belong to the class itself rather than instances of the class.

    • You can declare static properties and methods using the static keyword.

    • Static members can be accessed without creating an object of the class.

    class Circle {
      static const double pi = 3.14159;
      static int numberOfCircles = 0;

      Circle() {
        numberOfCircles++;
      }

      static void printPi() {
        print("The value of pi is: $pi");
      }
    }

    void main() {
      Circle();
      Circle();

      print("Number of circles: ${Circle.numberOfCircles}"); // Output: Number of circles: 2

      Circle.printPi(); // Output: The value of pi is: 3.14159
    }
  1. Mixins:

    • Mixins are a way to reuse a set of methods in multiple classes without using inheritance.

    • They allow you to add behavior to a class without becoming its superclass.

    • You can define a mixin using the mixin keyword and apply it to a class using the with keyword.

    mixin Swimmer {
      void swim() {
        print("Swimming...");
      }
    }

    mixin Flyer {
      void fly() {
        print("Flying...");
      }
    }

    class Animal {
      void eat() {
        print("Eating...");
      }
    }

    class Dolphin extends Animal with Swimmer {}

    class Bird extends Animal with Swimmer, Flyer {}

    void main() {
      var dolphin = Dolphin();
      dolphin.eat(); // Output: Eating...
      dolphin.swim(); // Output: Swimming...

      var bird = Bird();
      bird.eat(); // Output: Eating...
      bird.swim(); // Output: Swimming...
      bird.fly(); // Output: Flying...
    }

This guide covers the key concepts of object-oriented programming in Dart. By understanding these concepts and their implementation in Dart, you can build robust and scalable applications.

Thank you for reading! Stay connected for more valuable content. Be sure to follow us to receive regular updates, exclusive content, and special offers. Join our vibrant community of learners by visiting our website and exploring all that we have to offer. Let's continue our journey of knowledge and growth together!!

Did you find this article valuable?

Support Ratish Jain by becoming a sponsor. Any amount is appreciated!