×

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 
} 

Related Topics

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 minutes read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Enumerations

Enumeration data type in Dart In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword.  Implementation =  enum enum_name {    ...

2 minutes read.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.

Dart Features

Dart is a fully-featured, object-oriented modern language with its roots in the programming language ‘Smalltalk’. It has some of its eminent features similar to the languages such as Java, C#...

4 minutes read.

Dart Strings

Strings data type in Dart A Dart string is a sequence of characters with an encoding of UTF-16. The text associated with string data type is enclosed withing single (‘  ’)...

3 minutes read.

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,...

4 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

4 minutes read.

Dart Variables

Variables are used to store the value of a particular data type referred to in the whole program by a name or identifier. They refer to a memory location as...

5 minutes read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

4 minutes read.

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

4 minutes read.

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.

Dart Future and Stream

Before understanding Future and Stream in Dart, we need first to get familiar with two terms : Synchronous and Asynchronous Programming.  In synchronous programming, a single operation is handled at a...

9 minutes read.

Dart If statement

If statement is used to set the control on the lines of code. Using if statement, block of code is executed only if the expression in the statement returns true....

1 minute read.

Dart Operators Precedence and Associativity

Precedence of operators determines the order in which the operators are evaluated if they are grouped together in a sentence. If two operators share an operand then the one with...

5 minutes read.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.