Blogs By Shankar
Dart
Dart
  • Dart Tutorial by Dès Vu Technologies
  • hello
  • Dart Collection
    • collectionInDart
  • File Handeling
    • fileHandelingInDart
  • Functions
    • functionParameters
    • Types of Functions in Dart
    • Functions in Dart
    • Annonymous Function
      • AnnonymousFunction
    • Arrow Function
      • arrowFunctions
    • BuiltinFunctions
      • importantBuiltinFunctions
      • mathFunction
  • OOP With Dart
    • encapsulation
    • oopWithDart
    • Generic In Dart
      • generic
    • constructor
      • constructor
      • factory constructor
        • factoryConstructor
      • initializer list constructor
        • Initializer List Constructor:
    • async dart
      • asyncAndAwait
        • asyncAndAwait
      • future
        • future
      • streams
        • streams
  • Sync and Async dart
    • syncAndAsyncDart
  • controlFlow
    • controlFlow
  • dataTypes
    • Dart Built-In Data Types
    • TypeConversion
    • String
      • stringOperations
      • string_jnterpolation
    • operators
      • Operators In Dart
  • operators
    • operators
  • user_input
    • userInput
  • variablesAndConstants
    • scopeInDart
    • variableTypesInDart
    • variables
Powered by GitBook
On this page
  1. OOP With Dart

oopWithDart

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to organize and structure code. Dart is a modern language that fully supports OOP principles. Here's an overview of OOP concepts in Dart:

  1. Classes and Objects:

    • In Dart, everything is an object, and classes define the blueprint for creating objects.

    • Classes encapsulate data (attributes) and behavior (methods) into a single unit.

    • Objects are instances of classes and represent real-world entities.

    class Person {
      String name;
      int age;
    
      void introduce() {
        print('My name is $name, and I am $age years old.');
      }
    }
    
    void main() {
      var person = Person();
      person.name = 'Alice';
      person.age = 30;
      person.introduce();
    }
  2. Constructor:

    • Constructors are special methods used to initialize objects when they are created.

    • Dart provides a default constructor if one is not defined.

    class Person {
      String name;
      int age;
    
      // Parameterized constructor
      Person(this.name, this.age);
    
      void introduce() {
        print('My name is $name, and I am $age years old.');
      }
    }
    
    void main() {
      var person = Person('Alice', 30);
      person.introduce();
    }
  3. Inheritance:

    • Dart supports single inheritance, where a class can inherit properties and methods from a single parent class.

    • The extends keyword is used for inheritance.

    class Student extends Person {
      String school;
    
      Student(String name, int age, this.school) : super(name, age);
    
      void study() {
        print('$name is studying at $school.');
      }
    }
    
    void main() {
      var student = Student('Bob', 20, 'ABC School');
      student.introduce(); // Inherited from Person class
      student.study();
    }
  4. Polymorphism:

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

    • Method overriding enables subclass methods to provide a specific implementation.

    class Shape {
      void draw() {
        print('Drawing a shape.');
      }
    }
    
    class Circle extends Shape {
      @override
      void draw() {
        print('Drawing a circle.');
      }
    }
    
    void main() {
      Shape shape = Circle();
      shape.draw(); // Calls Circle's draw method
    }
  5. Encapsulation:

    • Encapsulation is the practice of hiding the internal details of an object and exposing only what is necessary.

    • Dart uses private and public access modifiers (_ and final/var) to control visibility.

    class BankAccount {
      double _balance = 0.0; // Private field
    
      void deposit(double amount) {
        if (amount > 0) {
          _balance += amount;
        }
      }
    
      double getBalance() {
        return _balance;
      }
    }
  6. Abstraction:

    • Abstraction allows you to define the structure of a class without providing a complete implementation.

    • Abstract classes and methods are defined using the abstract keyword.

    abstract class Shape {
      void draw(); // Abstract method
    }
    
    class Circle extends Shape {
      @override
      void draw() {
        print('Drawing a circle.');
      }
    }

These are the core OOP concepts in Dart. You can use them to create well-structured and maintainable code, making it easier to model real-world entities and their interactions in your Dart applications.

PreviousencapsulationNextGeneric In Dart

Last updated 1 year ago