×

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 Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 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 Comments

Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans...

2 minutes read.

Dart Basics

Dart, as earlier mentioned multiple times, is very similar to C, C++ and Java. It is an object-oriented, garbage collecting and class-based programming language. Let’s look further and see what Dart has...

10 minutes read.

Dart Arithmetic Operators

Arithmetic Operators comprises of all the operators that are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc. They are binary operators that work upon two operands. Consider A...

2 minutes read.

Dart Type Test Operators

Type Test Operators in Dart These operators are used to check the types of expressions at runtime. Dart is a typed language, and we often want to assert that a value...

1 minute 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 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 Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

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

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

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

6 minutes read.

Dart Runes and Graphemes

Runes and Graphemes data types in Dart Runes and Graphemes Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each...

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

Dart Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

5 minutes read.

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes read.