OOPS in Dart

Learn via video courses
Topics Covered

Overview

Object-Oriented Programming (OOP) in Dart is a programming paradigm that structures code around objects, combining data and methods to model real-world entities. Dart, a language developed by Google, supports key OOP concepts such as classes, objects, inheritance, polymorphism, and encapsulation. Classes act as blueprints for objects, enabling code reuse and modularity. Inheritance allows the creation of hierarchies to share characteristics among classes, while polymorphism permits the same method to be applied to different objects. Encapsulation hides implementation details and exposes only necessary functionalities. OOP in Dart fosters organized and maintainable code, enhancing software development efficiency.

Introduction

OOPs in Dart is a powerful paradigm that brings structure and elegance to software development. OOP in Dart revolves around the concept of creating classes that encapsulate data and behavior, allowing us to model real-world entities with ease. By defining classes and objects, we can organize code into reusable components, making our applications more maintainable and scalable. Inheritance empowers us to build hierarchies, promoting code reuse and modularity, while polymorphism enables flexible and dynamic behavior.Embracing OOPin Dart opens a gateway to writing robust, efficient, and extensible software solutions, propelling us towards programming mastery.

Four Pillars of OOPs in Dart

The four pillars of OOP are usually described as:

  1. Encapsulation: This means that the internal representation of an object is hidden from the outside. In Dart, you can create private fields by prefixing an underscore to the name, and you can use getters and setters to control access to those fields. This way, you can control how data within an object is accessed or modified.

  2. Inheritance: Inheritance allows a class to be defined that has a certain set of characteristics (fields, methods) and then other classes to be created that are derived from that class. The derived class inherits the characteristics of the base class and typically adds to or modifies them. Dart supports single inheritance, meaning that a class can only inherit from one parent class.

  3. Polymorphism: Polymorphism allows objects of different classes to be treated as objects of a common superclass. There are two types of polymorphism: compile-time (or static) and runtime (or dynamic). In Dart, you can make use of polymorphism through method overriding, where a method in a subclass can provide a different implementation for a method that is already defined in its superclass.

  4. Abstraction: Abstraction means dealing with the level of detail that is most appropriate to a task, and it often involves creating abstract classes that can't be instantiated on their own. These abstract classes are meant to be subclassed by other classes that provide concrete implementations of the abstract methods. Dart provides the abstract keyword to define abstract classes and methods.

Example:

This code defines an abstract Shape class and a Rectangle class that inherits from Shape. The Shape class encapsulates the details of how area and perimeter are calculated, and it provides an abstract interface for these properties. The Rectangle class provides a concrete implementation of the Shape interface, thus demonstrating polymorphism.

Classes in Dart

In Dart, class is a fundamental concept used for object-oriented programming. They serve as a blueprint for creating objects, which are instances of the class. Dart is an object-oriented language, so classes play a crucial role in structuring and organizing code.

  • Class Declaration: A class is declared using the class keyword, followed by the class name. The class body contains the class members like fields, constructors, methods, and getters/setters.
  • Fields: Fields are variables that belong to a class and define the state or characteristics of objects created from the class.
  • Constructors: Constructors are special methods used to initialize objects of the class. Dart provides a default constructor if you don't define one explicitly.
  • Methods: Methods are functions defined within the class, allowing objects to perform actions or behaviors.
  • Getters and Setters: Getters and setters are used to access and modify the private fields of a class, respectively. They provide controlled access to the fields.
  • Inheritance: Dart supports inheritance, allowing you to create a new class based on an existing class. The new class inherits the properties and behaviors of the parent class and can override or extend them.

Objects in Dart

In Dart, objects are instances of classes. They represent real-world entities or concepts and encapsulate data (fields) and behavior (methods) related to those entities. Objects are the primary building blocks of object-oriented programming in Dart.

  • Object Creation: To create an object, you need to instantiate a class using the new keyword (optional in Dart 2.0+).
  • Accessing Members: Once you create an object, you can access its fields and methods using the dot notation.
* **Constructors:** When creating an object, constructors are used to initialize the object with specific values.
```dart
    class Person {
      String name;

      // Custom constructor
      Person(this.name);
    }

    void main() {
      var person = Person('Alice'); // Creating an object using the constructor
      print(person.name);          // Output: Alice
    }
  • Methods: Objects can have methods that define their behavior and allow them to perform actions.
  • Identity and Equality: Each object in Dart has a unique identity, which means no two objects are the same unless they refer to the same memory location. You can check if two objects are the same using the identical() function. On the other hand, objects can be considered equal based on their content using the == operator, which can be overridden in the class.

Objects in Dart allow you to represent complex data structures, model real-world entities, and implement sophisticated behaviors in an organized and efficient manner. Understanding classes and objects is crucial for effective use of object-oriented programming paradigms in Dart.

Encapsulation in Dart

Encapsulation refers to the bundling of data and the methods that operate on that data, restricting the direct access to some of an object's components. This means that the internal representation of an object is hidden from the outside, and access to it is provided only through specific methods, properties, or functions.

In Dart, you can achieve encapsulation by using private and public access specifiers.

Public and Private Modifiers

  1. Public: By default, all members (fields, methods, getters, and setters) are public in Dart. They can be accessed from outside the class.
  2. Private: You can make a member private by prefixing its name with an underscore _. Private members can only be accessed within the same library where they are defined.

Here's a simple example illustrating encapsulation in Dart:

Here, _balance is encapsulated within the BankAccount class. It cannot be directly accessed or modified from outside the class. Instead, the public methods getBalance and deposit are provided to interact with the balance, enforcing control over how it can be accessed and changed.

Encapsulation helps in maintaining the integrity of the data by allowing control over the operations that can be performed on it, which leads to more robust and maintainable code.

Inheritance in Dart

Inheritance is a key concept in object-oriented programming, allowing for code reusability, and Dart is no exception. In Dart, inheritance is used to create a new class that is a modified version of an existing class. The new class inherits properties and behavior (data members and methods) from the existing class.

  1. Base Class (Parent Class): The class whose properties and functions are inherited by another class.

  2. Derived Class (Child Class): The class that inherits the properties and functionality from another class.

  3. 'extends' Keyword: In Dart, you use the extends keyword to create a class that inherits from another class.

  4. 'override' Keyword: If you need to redefine a method in a derived class, you can use the override keyword.

  5. Calling the Superclass Constructor: You can call the parent class's constructor using the super keyword.

Example Here's a simple example illustrating inheritance in Dart:

In this example, the Dog class extends the Animal class, so it inherits the eat method from Animal. We've used the @override annotation to provide a different implementation of the eat method specifically for the Dog class.

  • In Dart, only single inheritance is allowed, meaning a class can extend only one other class.
  • You can achieve a kind of multiple inheritance using "mixins" in Dart, but that's a separate concept.
  • Inheritance allows for the creation of hierarchical class relationships, making it easier to manage and scale code.
  • Inheritance in Dart helps in building more reusable and organized code by allowing derived classes to leverage the functionality of their base classes, possibly extending or modifying those behaviors as needed.

Polymorphism in Dart

Polymorphism in Dart refers to the ability of a class or data type to take on multiple forms or behave differently depending on the context. Dart is an object-oriented programming language, and polymorphism is one of the fundamental principles of object-oriented programming.

There are two main types of polymorphism in Dart:

  1. Compile-time Polymorphism (Static Polymorphism):
  • Operator Overloading: Dart allows you to redefine the behavior of operators for custom classes. By implementing specific methods like operator +, operator -, etc., you can define how the operator should behave for objects of that class.

In this example, we have defined the operator for the Vector class. The operator + method allows us to add two Vector objects using the + operator, providing a custom implementation for vector addition.

  1. Run-time Polymorphism (Dynamic Polymorphism):
  • Method Overriding: Inheritance in Dart enables a subclass to provide its own implementation of a method that is already defined in the superclass. When a method is called on an object of the subclass, the overridden method in the subclass is executed instead of the one in the superclass. Example:

Output

In this example, we have a base class Animal with a method makeSound(). Both Dog and Cat are subclasses of Animal and override the makeSound() method. During runtime, the method of the specific object type is called, demonstrating run-time polymorphism.

Polymorphism allows developers to write more flexible and reusable code. By using polymorphism, you can write code that works with a variety of different objects without needing to know their specific types, which enhances code modularity and maintainability.

Here's a simple example of polymorphism in Dart:

Output

In this example, the Shape class is the base class, and Circle and Square are subclasses that override the draw() method. During runtime, the correct draw() method is invoked based on the type of object, demonstrating the concept of run-time polymorphism.

Abstraction in Dart

Abstraction in Dart is a fundamental concept that allows us to hide complex details and display only necessary information. This can be achieved by using classes and interfaces. Dart provides the 'abstract' keyword to declare abstract classes, which can have abstract methods (methods without implementation). Abstract classes can't be instantiated, but they can be extended by other classes to provide specific implementation for those methods. This encapsulates complexity and promotes reusability and flexibility of code. Example:

Mixins in Dart

Mixins are a way of reusing a class's code in multiple class hierarchies. In Dart, mixins allow you to include the body of one class inside another without extending it. They are used to provide a common set of functionalities across different classes without duplicating code.

  1. Definition: Mixins are defined using the mixin keyword followed by the mixin name.

  2. Reuse: You can use the with keyword to include a mixin in a class.

  3. Constraints: Mixins can also extend other classes or implement interfaces. If you want to make sure that a mixin can only be used by classes that extend or implement certain classes, you can use the on keyword.

  4. Example:

  1. Advantages: Mixins provide a flexible and modular approach to code organization. They help in achieving code reusability and maintaining a clean class hierarchy.

  2. Limitations: Before Dart 2.1, mixins could only be applied to classes that extended Object. Now, there are no such restrictions, but you still need to be cautious with mixin usage to avoid conflicts and ambiguities in method resolution.

Mixins in Dart provide a powerful tool for code reuse, allowing developers to share methods and attributes among different classes without the constraints of single inheritance.

Interfaces

Interfaces in Dart are a way to ensure that a class adheres to a contract. Interestingly, Dart doesn't have a special syntax for interfaces like some other languages. Instead, every class implicitly defines an interface containing all the instance members of the class, and you can use a class as an interface by implementing the members.

  1. Implicit Interfaces: As mentioned, every class automatically defines an interface with its instance members (methods, getters, and setters). Any class can implement this interface by providing concrete implementations for those members.

  2. implements Keyword: To use a class as an interface, the implements keyword is used. A class can implement one or more interfaces.

  3. Contracts: By implementing an interface, the class must provide concrete implementations for all the members of the interface. If it fails to do so, the Dart analyzer will raise an error.

  4. Mixing extends and implements: A Dart class can also extend another class and implement one or more interfaces. The class must then fulfill the contract of the implemented interfaces while also inheriting behavior from the extended class.

  5. No Conflict with Multiple Interfaces: Dart allows implementing multiple interfaces in a single class. If there's a conflict (i.e., two interfaces contain a method with the same name), the implementing class must provide a concrete implementation that satisfies both interfaces.

Here's an example to illustrate the concept:

In the above example, class C implements the interfaces defined by classes A and B. Therefore, class C must provide concrete implementations for the showA and showB methods.

Conclusion

  • Dart's support for Object-Oriented Programming (OOP) provides a robust and flexible approach to designing and organizing code.

  • The use of classes and objects in Dart allows for encapsulation, abstraction, and modularity, leading to more maintainable and scalable applications.

  • Inheritance and polymorphism in Dart facilitate code reuse, reducing redundancy and enhancing code readability.

  • Dart's implementation of interfaces and abstract classes enables the creation of well-defined contracts, promoting a clearer understanding of class hierarchies.

  • OOP in Dart promotes a clear separation of concerns, making it easier to collaborate in larger development teams and manage complex projects.

  • By leveraging OOP principles in Dart, developers can build efficient, extensible, and structured applications that are easier to maintain and adapt to changing requirements.