OOP in Flutter — In Depth
<h1><strong>1. Class and Object — </strong>Blueprint for the Future<strong>:</strong></h1>
<p>In OOP, a class serves as a blueprint or template to create objects. It defines the properties (attributes) and methods (behavior) that the objects of the class will possess. An object, on the other hand, is an instance of a class. In Dart, classes are defined using the <code>class</code> keyword.</p>
<pre>
class Person {
String name;
int age;
void sayHello() {
print("Hello, I'm $name!");
}
}
void main() {
var person1 = Person();
person1.name = "John";
person1.age = 30;
person1.sayHello(); // Output: "Hello, I'm John!"
}</pre>
<p>— — —</p>
<h1>2. Encapsulation — Securing the Treasure:</h1>
<p>Encapsulation is the concept of bundling data (attributes) and methods (behavior) together within a class, hiding the internal implementation details from the outside world. It promotes data security and reduces code complexity. In Dart, you can use access modifiers like <code>public</code>, <code>private</code>, and <code>protected</code> to control the visibility of class members.</p>
<pre>
class BankAccount {
double _balance; // Private attribute
double get balance => _balance; // Getter for balance
void deposit(double amount) {
_balance += amount;
}
void withdraw(double amount) {
if (_balance >= amount) {
_balance -= amount;
} else {
print("Insufficient funds!");
}
}
}
void main() {
var account = BankAccount();
account.deposit(1000);
print("Balance: \$${account.balance}"); // Output: "Balance: $1000.0"
account.withdraw(500);
print("Balance: \$${account.balance}"); // Output: "Balance: $500.0"
}</pre>
<p>— — —</p>
<h1>3. Inheritance — Reusing the Power:</h1>
<p>Inheritance is a mechanism where a class (subclass or derived class) inherits properties and methods from another class (superclass or base class). It promotes code reusability and hierarchical organization. In Dart, you can use the <code>extends</code> keyword to create a subclass.</p>
<pre>
class Animal {
String name;
void speak() {
print("Animal makes a sound");
}
}
class Dog extends Animal {
@override
void speak() {
print("Dog barks");
}
}
void main() {
var dog = Dog();
dog.name = "Buddy";
dog.speak(); // Output: "Dog barks"
}</pre>
<p>— — —</p>
<h1>4. Multiple Inheritance:</h1>
<p>Multiple inheritance refers to a situation where a class can inherit properties and behavior from more than one superclass. In some programming languages, like C++, multiple inheritance is supported directly, but in Dart, it is achieved through the use of mixins.</p>
<pre>
class A {
void methodA() {
print("Method from A");
}
}
class B {
void methodB() {
print("Method from B");
}
}
class C with A, B {
void methodC() {
print("Method from C");
}
}
void main() {
var c = C();
c.methodA(); // Output: "Method from A"
c.methodB(); // Output: "Method from B"
c.methodC(); // Output: "Method from C"
}</pre>
<p>In the example above, class <code>C</code> uses the <code>with</code> keyword to mixin both classes <code>A</code> and <code>B</code>, allowing it to inherit the methods from both superclasses.</p>
<h1>5. Multilevel Inheritance:</h1>
<p>Multilevel inheritance refers to a situation where a class can inherit from another class, which in turn inherits from yet another class. It creates a chain of inheritance, and the derived class has access to properties and methods from all its ancestors.</p>
<p><a href="https://medium.com/@ahsan-001/oop-in-flutter-in-depth-6cbf40640d69">Read More</a></p>