Object oriented concept in dart /OOPs in dart

Dart is an object-oriented programming language, and it supports all the concepts of object-oriented programming such as classes, object, inheritance, mixin, and abstract classes. As the name suggests, it focuses on the object and objects are the real-life entities.

The Object-oriented programming approach is used to implement the concept like polymorphism, data-hiding, etc. The main goal of oops is to reduce programming complexity and do several tasks simultaneously. The oops concepts are given below.

  • Class
  • Object
  • Inheritance
  • Polymorphism
  • Interfaces
  • Abstract class

Class

Dart classes are defined as the blueprint of the associated objects. A Class is a user-defined data type that describes the characteristics and behavior of it. To get all properties of the class, we must create an object of that class. The syntax of the class is given below.

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

Object

An object is a real-life entity such as a table, human, car, etc. The object has two characteristics – state and behavior. Let’s take an example of a car which has a name, model name, price and behavior moving, stopping, etc. The object-oriented programming offers to identify the state and behavior of the object.

We can access the class properties by creating an object of that class. In Dart, The object can be created by using a new keyword followed by class name. The syntax is given below.

var objectName = new ClassName(<constructor_arguments>)  

Inheritance

Dart supports inheritance, which is used to create new classes from an existing class. The class that to be extended is called parent /superclass, and the newly created class is called child/subclass. Dart provides extends keyword to inherit the properties of parent class in child class. The syntax is given below.

class child_class_name extends parent_class_name   

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 the ability of an object to take on many forms. This allows objects to be treated as objects of their parent class, even if they are instances of a child class. In Dart, polymorphism is achieved through method overriding, where a child class can provide a different implementation of a method that is inherited from its parent class. For example:

class Manager extends Employee {
String department;

@override
void displayEmployeeDetails() {
super.displayEmployeeDetails();
print("Manager Department: $department");
}
}

These are the core OOP concepts in Dart, and they provide a powerful toolset for building complex applications. By organizing code into classes, objects, and creating relationships through inheritance and polymorphism, developers can build robust, scalable, and maintainable applications.

In conclusion, Dart is a statically-typed, object-oriented programming language that supports key OOP concepts, including classes, objects, inheritance, and polymorphism. These concepts provide a powerful toolset for building complex applications, and they help developers to create robust, scalable, and maintainable code. Whether you’re building a web, server, or mobile application, Dart’s support for OOP concepts is an essential tool for success.

nterfaces

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 
} 

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More