oopWithDart
Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to organize and structure code. Dart is a modern language that fully supports OOP principles. Here's an overview of OOP concepts in Dart:
Classes and Objects:
In Dart, everything is an object, and classes define the blueprint for creating objects.
Classes encapsulate data (attributes) and behavior (methods) into a single unit.
Objects are instances of classes and represent real-world entities.
class Person { String name; int age; void introduce() { print('My name is $name, and I am $age years old.'); } } void main() { var person = Person(); person.name = 'Alice'; person.age = 30; person.introduce(); }
Constructor:
Constructors are special methods used to initialize objects when they are created.
Dart provides a default constructor if one is not defined.
class Person { String name; int age; // Parameterized constructor Person(this.name, this.age); void introduce() { print('My name is $name, and I am $age years old.'); } } void main() { var person = Person('Alice', 30); person.introduce(); }
Inheritance:
Dart supports single inheritance, where a class can inherit properties and methods from a single parent class.
The
extends
keyword is used for inheritance.
class Student extends Person { String school; Student(String name, int age, this.school) : super(name, age); void study() { print('$name is studying at $school.'); } } void main() { var student = Student('Bob', 20, 'ABC School'); student.introduce(); // Inherited from Person class student.study(); }
Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of a common superclass.
Method overriding enables subclass methods to provide a specific implementation.
class Shape { void draw() { print('Drawing a shape.'); } } class Circle extends Shape { @override void draw() { print('Drawing a circle.'); } } void main() { Shape shape = Circle(); shape.draw(); // Calls Circle's draw method }
Encapsulation:
Encapsulation is the practice of hiding the internal details of an object and exposing only what is necessary.
Dart uses private and public access modifiers (
_
andfinal
/var
) to control visibility.
class BankAccount { double _balance = 0.0; // Private field void deposit(double amount) { if (amount > 0) { _balance += amount; } } double getBalance() { return _balance; } }
Abstraction:
Abstraction allows you to define the structure of a class without providing a complete implementation.
Abstract classes and methods are defined using the
abstract
keyword.
abstract class Shape { void draw(); // Abstract method } class Circle extends Shape { @override void draw() { print('Drawing a circle.'); } }
These are the core OOP concepts in Dart. You can use them to create well-structured and maintainable code, making it easier to model real-world entities and their interactions in your Dart applications.
Last updated