OOP in Flutter — In Depth

<h1><strong>1. Class and Object &mdash;&nbsp;</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&nbsp;<code>class</code>&nbsp;keyword.</p> <pre> class Person { String name; int age; void sayHello() { print(&quot;Hello, I&#39;m $name!&quot;); } } void main() { var person1 = Person(); person1.name = &quot;John&quot;; person1.age = 30; person1.sayHello(); // Output: &quot;Hello, I&#39;m John!&quot; }</pre> <p>&mdash; &mdash; &mdash;</p> <h1>2. Encapsulation &mdash; 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&nbsp;<code>public</code>,&nbsp;<code>private</code>, and&nbsp;<code>protected</code>&nbsp;to control the visibility of class members.</p> <pre> class BankAccount { double _balance; // Private attribute double get balance =&gt; _balance; // Getter for balance void deposit(double amount) { _balance += amount; } void withdraw(double amount) { if (_balance &gt;= amount) { _balance -= amount; } else { print(&quot;Insufficient funds!&quot;); } } } void main() { var account = BankAccount(); account.deposit(1000); print(&quot;Balance: \$${account.balance}&quot;); // Output: &quot;Balance: $1000.0&quot; account.withdraw(500); print(&quot;Balance: \$${account.balance}&quot;); // Output: &quot;Balance: $500.0&quot; }</pre> <p>&mdash; &mdash; &mdash;</p> <h1>3. Inheritance &mdash; 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&nbsp;<code>extends</code>&nbsp;keyword to create a subclass.</p> <pre> class Animal { String name; void speak() { print(&quot;Animal makes a sound&quot;); } } class Dog extends Animal { @override void speak() { print(&quot;Dog barks&quot;); } } void main() { var dog = Dog(); dog.name = &quot;Buddy&quot;; dog.speak(); // Output: &quot;Dog barks&quot; }</pre> <p>&mdash; &mdash; &mdash;</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(&quot;Method from A&quot;); } } class B { void methodB() { print(&quot;Method from B&quot;); } } class C with A, B { void methodC() { print(&quot;Method from C&quot;); } } void main() { var c = C(); c.methodA(); // Output: &quot;Method from A&quot; c.methodB(); // Output: &quot;Method from B&quot; c.methodC(); // Output: &quot;Method from C&quot; }</pre> <p>In the example above, class&nbsp;<code>C</code>&nbsp;uses the&nbsp;<code>with</code>&nbsp;keyword to mixin both classes&nbsp;<code>A</code>&nbsp;and&nbsp;<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>
Tags: Flutter Depth