×

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

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

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

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the...

3 minutes read.

Dart Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute read.

Dart Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

3 minutes read.

Dart Anonymous Function

Dart functions are the user defined functions that are designed to perform specific task. The use case of the functions is that they can be directly called any number of...

3 minutes read.

Dart If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then...

1 minute read.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 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 Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

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 Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 minutes read.

Dart Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 minutes read.

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.

Dart Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

4 minutes read.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

2 minutes read.

Dart Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

2 minutes read.