Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

Dart Object-Oriented Concepts

Dart is a programming language that supports object-oriented programming. It is focused on the objects that are real-world entities and supports all the concepts of OOPS, such as objects, classes, data abstraction, data encapsulation, inheritance, abstract classes, and mixins. 

The support of Object-oriented programming lets us implement various concepts like data-hiding, polymorphism, etc. The main objective of Object-oriented programming (OOPS) is to bind together the data and its functions as a single unit to do many tasks simultaneously and reduce the complexity of the programs.

Some of the OOPS concepts are as follows :

  • Classes
  • Object
  • Inheritance
  • Polymorphism
  • Interfaces
  • Abstract class

Let us study each one of them in detail.

Classes

Classes are also called the blueprints of the objects. They are the user-defined data type that encapsulates the data members and their functions as a single unit. They define the characteristics and behaviour of the objects. Every class in Dart is inherited from the 'Object' class. 

Static members of the class are declared using the 'static' keyword. All the members that are not initialized using static keywords are considered the instance variables. 'this' keyword used inside the member function of a class points to the object itself. 

To access the class members, we create objects of that class. 

The classes in Dart can contain following things :

  1. Fields
  2. Methods
  3. Constructors
  4. User Defined Methods / Functions

Syntax :

class ClassName { 
< fields > 
< getter / setter > 
< constructor > 
< functions > 
} 

The class keyword is used to declare and define the class in Dart. 

Objects

The object is any real-world entity like a student, car, pencil, etc. Objects have two main characteristics - their state and behaviour. These characteristics can be identified and explained using object-oriented programming concepts. 

The object is also referred to as an instance of the class as we can access the properties of the class using objects. 

For example, 

A student is a real-world entity with properties like name, class, roll no, etc.

An object in Dart can be initialized using the 'new' keyword followed by the Class name. 

Syntax :

var object_name = new class_name ( < constructor_arguments > )

Inheritance

Inheritance is a property by which a class derives the properties of an existing class. The class deriving the property is known as the derived class or subclass or child class, while the existing class is known as the base class or superclass or parent class. The keyword 'extends' is used while inheriting a class. Inheritance can be single or multiple.

All the members of the classes are inherited except the static properties and constrictor functions. 

This is the most wonderful feature of object-oriented programming. At times, some of the classes share similar methods and instance variables. In that case, instead of maintaining two classes with common functionalities, we can have one class inherit the functionality of the other class. This helps preserve the existing features and add more functionality or moderations to them. 

Inheritance represents the IS-A relationship, also been called a parent-child relationship.

Syntax :

class child_class_name extends parent_class_name 

Uses of Inheritance :

1. Code Reusability

2. Method Overriding

Polymorphism

Polymorphism is made up of two Greek words, 'Poly', which means many, 'morphism', which means forms. 

Polymorphism which means many forms is a property that can have many forms. In terms of programming, it is a way by which different functions can have the same name but different functionality. It permits the programmer to generate high-level reusable components tailored to fit different applications by changing their low-level parts.

For example – 

Two functions with the same name but with different behaviour or class. Another example is the vehicle( ) class, and all the classes are inherited from the TwoWheeler, ThreeWheeler, and FourWheeler.

@override notation denotes that the method is overridden or is performing polymorphism. 

Interfaces

The Interface is defined as the blueprint of the class that every class should abide by if it interfaces that class. 

In Interfaces, the declaration of methods and variables is the same as that in a class, but an additional feature is that the abstract declaration of methods is also provided.

Only the function signature can be defined inside an interface but not the function's body. Another class can implement the Interface. Its basic functionality is for data-hiding.

Syntax :

class Interface_class_name {
   . . .
}


class Class_name implements Interface_class_name {
   . . .
}

Abstract Class

Any class containing one or more abstract methods in Dart is known as Abstract Class. Abstract Class can be declared using the 'abstract' keyword followed by the class declaration in Dart.

An important point to remember here is that a class declared as abstract can't be initialized. It can be extended, and if it is inherited, It is important to mention all its abstract methods with implementation. 

Syntax:

abstract class ClassName { 
// Body of abstract class 
}