OOPS in Dart
Object-Oriented Programming (OOP) in Dart is a fundamental programming paradigm that revolves around the concept of objects. Dart, being an object-oriented language, provides robust support for implementing OOP principles, enabling developers to create modular, scalable, and maintainable code.
In the below PDF we discuss about OOPS Concepts in detail in simple language, Hope this will help in better understanding.
Classes and Objects in dart:
The building blocks of object-oriented programming in Dart are classes and objects. A class serves as a blueprint for creating objects, defining their structure and behavior. Let’s consider a simple example:
class Person {
String name;
int age;
void speak() {
print('$name is $age years old.');
}
}
void main() {
var person1 = Person();
person1.name = 'John';
person1.age = 30;
person1.speak(); // Output: John is 30 years old.
}
In this example, we define a Person class with properties name and age, along with a speak() method. We then create an instance of the Person class and invoke the speak() method.
OOPS Concepts in Dart:
1. Encapsulation
Encapsulation is the practice of bundling data and methods that operate on the data within a single unit, i.e., a class. It helps in hiding the internal state of an object from the outside world and only exposing the necessary functionalities. Dart supports encapsulation through the use of access modifiers like public, private, and protected.
class BankAccount {
String _accountNumber; // Private field
double _balance = 0; // Private field
void deposit(double amount) {
_balance += amount;
}
void withdraw(double amount) {
_balance -= amount;
}
double get balance => _balance; // Getter for balance
}
In this example, _accountNumber and _balance are private fields accessible only within the BankAccount class.
2. Inheritance
Inheritance is a fundamental concept in OOP that allows a class (subclass) to inherit properties and methods from another class (superclass). Dart supports single inheritance, meaning a class can only inherit from one superclass.
class Animal {
void eat() {
print('Animal is eating.');
}
}
class Dog extends Animal {
void bark() {
print('Woof!');
}
}
void main() {
var dog = Dog();
dog.eat(); // Output: Animal is eating.
dog.bark(); // Output: Woof!
}
In this example, the Dog class inherits the eat() method from the Animal class.
3. Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass. This enables flexibility and extensibility in the codebase. Dart supports polymorphism through method overriding.
class Shape {
double area() {
return 0;
}
}
class Circle extends Shape {
double radius;
@override
double area() {
return 3.14 * radius * radius;
}
}
class Rectangle extends Shape {
double length;
double width;
@override
double area() {
return length * width;
}
}
In this example, both Circle and Rectangle classes override the area() method defined in the Shape class.
4. Abstraction:
Dart enables abstraction through the use of abstract classes and interfaces. Abstract classes cannot be instantiated directly and are meant to serve as blueprints for other classes.
abstract class Vehicle {
void start();
}
Conclusion:
In Conclusion, Object-oriented programming is a powerful paradigm that plays a crucial role in Dart programming. By understanding and effectively applying OOP principles such as classes, objects, encapsulation, inheritance, and polymorphism, developers can build maintainable, scalable, and efficient applications in Dart. Mastering these concepts opens up a world of possibilities, empowering developers to craft elegant solutions to complex problems.
Related Question
Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of objects, which can contain data and code to manipulate that data. It emphasizes modularity and reusability of code.
Constructor overloading in Dart refers to the ability to define multiple constructors within a class, each with a different number or type of parameters.
Inheritance in Dart allows a class to inherit properties and methods from another class. It is achieved using the extends keyword followed by the name of the superclass. Subclasses can override methods and properties of the superclass.
Encapsulation in Dart is the bundling of data and methods that operate on that data within a single unit, such as a class. It allows for better control over access to the data by making certain members private and exposing only necessary functionality through public methods.
Polymorphism in Dart is achieved through method overriding and method overloading. Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass, while method overloading allows defining multiple methods with the same name but different parameters in the same class or subclass.
Relevant
Queue in Dart Programming A
Maps in Dart A Map
Set in Dart A Set
Dart Lists A Dart list
Strings in Dart A Dart
Functions in Dart In Dart,
Exception Handling in Dart Exception