Conversation


Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0


Object-oriented Programming(OOP) in Dart Programming

OOP in Dart Programming


OOP in Dart

OOP is a programming approach where objects are used instead of logic. It incorporates concepts such as abstraction, encapsulation, polymorphism, and inheritance using classes, objects, and other features. OOP aims to reduce the complexity of programming and do several tasks simultaneously.


Applying OOP Concepts in Dart

Dart is a language that supports all the concepts of OOPs such as classes, objects, inheritance, polymorphism, interfaces, and abstraction. Once these concepts are understood, they can be applied to Dart easily.

The following keywords are important in applying OOP concepts: class, implement, extend, @override, and so on.


Types of OOP

There are two types of OOP in Dart:

  • Inheritance
  • Polymorphism
  • Interfaces
  • Abstraction

1. Inheritance:

The concept of a class inheriting the properties and methods of another class is called inheritance. The main class which has properties and methods is called the parent class and the one that inherits from the parent class is called the child class. The keyword extend is used to inherit properties from the parent class into the child class.

There are two types of inheritance in Dart:

  • Single-level inheritance: In single-level inheritance, one child class is inherited from one parent class.
  • Multi-level inheritance: In multi-level inheritance, a child class is inherited from another child class.

Syntax of Inheritance:

class parentclass{
...
}
//single level inheritance 
class childclass1 extends parentclass
{
...
}
//multi level inheritance 
class childclass2 extends childclass1{
...
}



Polymorphism:

Polymorphism refers to the concept of one object having many forms. Technically, polymorphism is extending and modifying the features of the child class using the already existing features of the parent class. This helps in saving time and reducing lines of code and effort.

Dart supports polymorphism through method overriding. A method in the parent class can be overridden in the method of the child class.

Syntax of Polymorphism by Method Overriding:

class parentclass{
void parentmethod(){
  //do something
 }
}
class childclass extends parentclass{
@override
void parentmethods(){
  //do something more
 }
}



Interfaces:

An interface is a blueprint for a class. In an interface, only an abstract declaration of the method will be provided.

Dart does not have an interface keyword. Every class implicitly defines an interface. This means that developers can implement any class.

In Dart, if a class is inherited, then all the methods of the inherited class should be redefined. To enforce this and ensure it is done, the implement keyword is used.

Syntax of Interface:

class classname{
...
returntype method(){

//some functionality
}
}
class classname implements Interfaceclassname
{
...
@override
returntype method(){
//different functionality than that which was defined in parent class
}
}



Abstraction:

In OOPs, abstraction means hiding unnecessary information and showing only important information to users. It reduces programming complexity. For example, knowledge of the backend process for sending a message is not necessary for a user to send a message.

In Dart, abstract classes are created using the abstract keyword. They have methods which may or may not have an implementation. The classes inheriting this abstract class can override the abstract methods. An abstract class can also be extended.

Example of Abstract class:

abstract class mainclass{
void abstractprintmethod();
}
class implementationclass implements mainclass{
@override
void abstractprintmethod(){
  //do something
  }
}

Dart OOP object-oriented programming types of OOP Inheritance Polymorphism Interfaces Abstraction
Blogs