Dart is a true Objected oriented programming language. In dart, everything is an object. It supports all kinds of oops concepts like Class, Object, Inheritance, Polymorphism, Encapsulation, Abstraction, etc. in addition to this dart also supports another key feature called mixin. The main purpose of mixin is to implement multiple class inheritance.
Let's discuss oops concepts in detail.
A class is a blueprint for creating an object. it encapsulates all its data members. it is represented with a keyword called class and followed by a class name.
Eg:
class SampleClass{
// Code
}
A class consists of Instance variables, user-defined methods, and constructors. Let's take a look at the below example on class and its declarations.
Consider an example of an employee. In a general way, any employee in an organization has three things most common they're Age, designation, and his/her name.
Note: The default value of an instance variable is null. It means that when we don't initialize the value, the value that is allocated to the variable is always null.
Eg:
void main(){
// Object declaration
Employee emp = new Employee();
// Here we're assigning the values for an employee
emp.age = 33;
emp.designation = 'Software Developer';
emp.name = 'Aditya';
emp.project();
emp.department();
}
// Properties of an employee and his behavior
class Employee {
// Instance Variables
int age = 0;
String name = '';
String designation = '';
// Declaring a method
project(){
print('$name working for mobile apps project');
}
department(){
print('$name is a $designation');
}
}
Let's discuss more on other oops concepts in detail.