<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by BG Aasmitha on Medium]]></title>
        <description><![CDATA[Stories by BG Aasmitha on Medium]]></description>
        <link>https://medium.com/@bgaasmitha?source=rss-115943537542------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*igtuCQjw0GVb58F_</url>
            <title>Stories by BG Aasmitha on Medium</title>
            <link>https://medium.com/@bgaasmitha?source=rss-115943537542------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 07:36:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bgaasmitha/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Collections in Dart (List, Set, Map, HashSet, HashMap)]]></title>
            <link>https://medium.com/@bgaasmitha/collections-in-dart-list-set-map-hashset-hashmap-8be0fa8f883b?source=rss-115943537542------2</link>
            <guid isPermaLink="false">https://medium.com/p/8be0fa8f883b</guid>
            <category><![CDATA[collections-in-java]]></category>
            <category><![CDATA[maps]]></category>
            <category><![CDATA[hashmap]]></category>
            <category><![CDATA[hashset]]></category>
            <category><![CDATA[lists]]></category>
            <dc:creator><![CDATA[BG Aasmitha]]></dc:creator>
            <pubDate>Tue, 03 Feb 2026 05:20:38 GMT</pubDate>
            <atom:updated>2026-02-03T05:20:38.009Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. List</h3><p>It is an ordered collection of elements where duplicates are allowed. Ex: In a shopping cart, we add a list of different products.</p><p>Typed List: List&lt;String&gt; fruits= [“mango”, “Apple”, “orange”] //hold items of particular data type.</p><p>Untyped List: List items = [“Alice”, 42, true] //hold items of different data type.</p><p>Empty list: List&lt;int&gt; numbers = [].</p><p>Time complexity: Insertion at end: O(1), Insert at start: O(n), Insert at middle: O(n), Delete at end: O(1), Delete at start: O(n), Delete at middle: O(n), Searching: O(n), Traversing: O(n).</p><p>Different operations that can be performed on list:</p><pre><br><br>void main()<br>{<br> <br>  List&lt;String&gt; cart = [];<br>  <br>  //1. to check whether list is empty or not<br>  print(cart.isEmpty);<br>  <br>  //2. to add items in list<br>  cart.add(&quot;android phone&quot;);<br>  cart.add(&quot;waterBottle&quot;);<br>  <br>  //3. to add more than one item in list<br>  cart.addAll([&quot;watch&quot;,&quot;perfume&quot;]);<br>  <br>  //4. to insert at particular position<br>  cart.insert(1,&quot;lipbalm&quot;);<br>  <br>  //5. update value in list<br>  cart[0] = &quot;ios phone&quot;;<br>  <br>  //6. remove an item in list<br>  cart.remove(&quot;perfume&quot;);<br>  <br>  //7. remove item at particular index in list<br>  cart.removeAt(2);<br>  <br>  //8. to check length of the list<br>  print(cart.length);<br>  <br>  //9. to reverse a list<br>  print(cart.reversed);<br>  <br>  //10. to retrieve item at particular index<br>  print(cart.elementAt(0));<br>  <br>  List&lt;int&gt; num = [1,2,6,5,4,3];<br>  <br>  <br>  //11. to return first element that matches the condition<br>  int ans = num.firstWhere((e)=&gt; e&gt;3);<br>  print(&quot;first element that matches condition : $ans&quot;);<br>  <br>  //12. to return last element that matches the condition<br>  int ans1 = num.lastWhere((e)=&gt; e&gt;4);<br>  print(&quot;last element that matches condition : $ans1&quot;);<br>  <br>  //13. to return only element matching the condition<br>  int ans2 = num.singleWhere((e)=&gt;e==3);<br>  print(ans2);<br>  <br>  //14. to return true if any element matches<br>   print(num.any((e)=&gt;e==3));<br>  <br>  //15. return true if all element matches condition<br>   print(num.every((e)=&gt;e&gt;7));<br>  <br>  //16. to take only first n elements<br>   print(num.take(2));<br>  <br>  //17. to take elements until condition meets<br>  print(num.takeWhile((e)=&gt; e&lt;5));<br>  <br>  // 18.to skip first n elements<br>  print(num.skip(1));<br>  <br>  //19.to skip elements until condition meets<br>   print(num.skipWhile((e)=&gt; e&lt;5));<br>}</pre><h3>2. Set</h3><p>It is an unordered collection of elements. Duplicates are not allowed. Example: Collection of Aadhar numbers.</p><p>Time complexity: By default set is implemented as HashSet so insert, delete, and search operations take O(1) on average.</p><p>Ways to declare set: Set&lt;int&gt; numbers = {1, 2, 3, 4}.</p><pre>void main()<br>{<br>  Set&lt;int&gt; num = {2,3,4,5,1};<br>  num.add(10);<br>  num.addAll({34,35});<br>  print(num);<br>  print(num.length);<br>  print(num.remove(3));<br>  print(num);<br>  <br>  print(num.contains(34));<br>  print(num.contains(90));<br>}</pre><p><strong>Empty set:</strong> Set&lt;String&gt; fruits = Set ().</p><pre><br>void main()<br>{<br>  Set&lt;String&gt; fruits = Set(); //Set&lt;String&gt; fruits = {} this is also empty set;<br>  fruits.add(&quot;apple&quot;);<br>  fruits.add(&quot;mango&quot;);<br>  fruits.add(&quot;apple&quot;);<br>  print(fruits);  <br>}</pre><p><strong>To convert List -&gt; set</strong></p><pre>void main()<br>{<br> <br>  List&lt;int&gt; num = [1,2,6,5,4,3,3,4,5];<br>  print(num);<br><br>  Set&lt;int&gt; set = num.toSet();<br>  print(set);<br>  <br>  set.add(30);<br>  set.add(23);<br>  <br>  print(set);<br><br>}output: [1, 2, 6, 5, 4, 3, 3, 4, 5] //in list duplicates are allowed.<br>{1, 2, 6, 5, 4, 3} //in set duplicates are not allowed.</pre><h3>3. Map</h3><p>Map is a dynamic collection of key value pairs. Keys and values in a map may be of any type. Keys must be unique.</p><p>Example: A college maintains a student database where each student has a <strong>unique roll number</strong>. Here roll number is key, student details are values.</p><p>Time complexity: By default Map is implemented as Linked HashMap so insert, delete, search operations take O(1) on average.</p><p>Map can be declared in two ways</p><p>3.1 Using literals.</p><pre>void main()<br>{<br>  //using literals<br>  <br>  var map = {&#39;1&#39;:&#39;leo&#39;,&#39;2&#39;:&#39;bruno&#39;};<br>  <br>  print(map[&#39;1&#39;]);<br>  <br>  map[&#39;3&#39;] = &#39;zack&#39;;<br>  <br>  print(map);<br>  <br>  print(map.length);<br>  <br>  print(map.isEmpty);<br>  <br>  print(map.isNotEmpty);<br>  <br>  print(map.containsKey(&#39;3&#39;));<br>  <br>  map.addAll({&#39;4&#39;:&#39;john&#39;,&#39;5&#39;:&#39;smith&#39;});<br>  print(map);<br>  <br>  map.remove(&#39;3&#39;);<br>  print(map);<br>  <br>  map.clear();<br>  <br>  print(map);<br>  <br>}</pre><p>3.2 Using constructors.</p><pre>void main()<br>{<br>  //using constructors<br>  <br>  var map = new Map();<br>  map[&#39;1&#39;] = &#39;leo&#39;;<br>  map[&#39;2&#39;]=&#39;john&#39;;<br>  map.addAll({&#39;3&#39;:&#39;jai&#39;,&#39;4&#39;:&#39;zack&#39;});<br>  print(map);<br>}</pre><h3>4.HashSet</h3><p>HashSet is used when we need faster lookup for elements. HashSet is an Unordered collection of elements, and no duplicate elements are allowed.</p><pre>import &#39;dart:collection&#39;;<br><br>void main() {<br>  HashSet&lt;int&gt; hs = HashSet();<br>  hs.add(10);<br>  hs.add(20);<br>  hs.add(10); // duplicate ignored<br>  print(hs);<br>  print(hs.length);<br>  print(hs.contains(30));<br>}</pre><h3>5.HashMap</h3><p>A HashMap stores key-value pairs. Internally it uses hashing, so operations like insert, search, delete are fast (O (1)). Used when we need faster lookup, insertion order is not important.</p><pre>import &#39;dart:collection&#39;;<br>void main() {<br>  <br>  HashMap&lt;int,String&gt; map = new HashMap();<br>  <br>  map[1] = &#39;apple&#39;;<br>  map[2] = &#39;banana&#39;;<br>  map[3] = &#39;orange&#39;;<br>  <br>  print(map);<br>  print(map.containsKey(4));<br>  print(map.containsValue(&#39;apple&#39;));<br>  print(map.length);<br>  print(map.remove(1));<br>  <br>  map.forEach((key,value){print(&quot;$key,$value&quot;);});<br><br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8be0fa8f883b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Encapsulation in Dart]]></title>
            <link>https://medium.com/@bgaasmitha/encapsulation-in-dart-9d1c46ae568e?source=rss-115943537542------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d1c46ae568e</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[encapsulation]]></category>
            <category><![CDATA[oops-concepts]]></category>
            <dc:creator><![CDATA[BG Aasmitha]]></dc:creator>
            <pubDate>Thu, 29 Jan 2026 05:10:28 GMT</pubDate>
            <atom:updated>2026-01-29T05:12:12.696Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. What is Encapsulation?</h3><p>Mechanism where it hides all the unnecessary details from the outside world and give controlled access to it. It can be achieved using private variables (Private variables are declared using _ and are accessible only within the same library (file)) along with getters and setters.</p><p><strong>Getters (read only)</strong>: Uses the keyword get. It is used to access the value of private variable.</p><pre>class Account {<br>  double _balance;<br><br>  Account(this._balance);<br><br>  double get balance =&gt; _balance;<br>}</pre><pre>import &#39;../account.dart&#39;;<br><br>void main() {<br>  Account a = Account(200);<br>  print(a.balance);      // allowed<br>  // print(a._balance);  // ERROR: private member<br>}</pre><p><strong>Setters:</strong> Uses the keyword set. It is used to update the value of private variable.</p><pre>class Account {<br>  double _balance;<br><br>  Account(this._balance);<br><br>  // getter<br>  double get balance =&gt; _balance;<br><br>  // setter<br>  set balance(double amount) {<br>    if (amount &lt; 0) {<br>      print(&quot;Balance cannot be negative&quot;);<br>    } else {<br>      _balance = amount;<br>    }<br>  }<br>}</pre><pre>import &#39;../lib/account.dart&#39;;<br><br>void main() {<br>  Account a = Account(200);<br><br>  print(a.balance);   // getter call<br><br>  a.balance = 500;    // setter call<br>  print(a.balance);<br><br>  a.balance = -100;   // invalid update<br>}</pre><h3>2. Why do we need Encapsulation?</h3><p><strong>Without Encapsulation: No data hiding from the outside world</strong>. Everyone can access the variables and change the variables, sometimes the variables may go to invalid state.</p><p><strong>With Encapsulation: Data hiding from the outside world. </strong>Variables can be accessed only using getters and setters. Data integrity is maintained.</p><h3>3. How to implement Encapsulation?</h3><p>Encapsulation can be implemented using private variables along with getters and setters.</p><pre>class BankAccount {<br>  // private variables <br>  String _accountNumber;<br>  String _holderName;<br>  double _balance;<br><br>  // constructor<br>  BankAccount(this._accountNumber, this._holderName, this._balance);<br><br>  // getter (read-only access)<br>  double get balance =&gt; _balance;<br><br>  String get holderName =&gt; _holderName;<br><br>  // deposit <br>  void deposit(double amount) {<br>    if (amount &gt; 0) {<br>      _balance += amount;<br>      print(&quot;₹$amount deposited successfully&quot;);<br>    } else {<br>      print(&quot;Invalid deposit amount&quot;);<br>    }<br>  }<br><br>  // withdraw <br>  void withdraw(double amount) {<br>    if (amount &lt;= 0) {<br>      print(&quot;Invalid withdrawal amount&quot;);<br>    } else if (amount &gt; _balance) {<br>      print(&quot;Insufficient balance&quot;);<br>    } else {<br>      _balance -= amount;<br>      print(&quot;₹$amount withdrawn successfully&quot;);<br>    }<br>  }<br>}</pre><pre>import &#39;../bank_account.dart&#39;;<br><br>void main() {<br>  BankAccount acc = BankAccount(<br>    &quot;AC12345&quot;,<br>    &quot;Aasmitha&quot;,<br>    5000,<br>  );<br><br>  print(&quot;Account Holder: ${acc.holderName}&quot;);<br>  print(&quot;Balance: ₹${acc.balance}&quot;);<br><br>  acc.deposit(2000);<br>  acc.withdraw(3000);<br><br>  print(&quot;Final Balance: ₹${acc.balance}&quot;);<br><br>  // NOT allowed (data is hidden)<br>  // acc._balance = 100000;<br>}</pre><h3>Advantages of Encapsulation</h3><p>Data Hiding (Security), Controlled Access, Reusability, Maintainability.</p><h3>Disadvantages of Encapsulation</h3><p>Increased Code Length, Hard to understand for beginners.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d1c46ae568e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Abstraction in Dart]]></title>
            <link>https://medium.com/@bgaasmitha/abstraction-in-dart-b8c7bbf73666?source=rss-115943537542------2</link>
            <guid isPermaLink="false">https://medium.com/p/b8c7bbf73666</guid>
            <category><![CDATA[oops-concepts]]></category>
            <category><![CDATA[java-abstract-classes]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[abstraction]]></category>
            <dc:creator><![CDATA[BG Aasmitha]]></dc:creator>
            <pubDate>Wed, 28 Jan 2026 05:51:36 GMT</pubDate>
            <atom:updated>2026-01-28T05:51:36.801Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is Abstraction?</h3><p>Abstraction means hiding all the unnecessary implementation details from the user and showing only what is required.</p><p>Example: the user wants to know whether the fan is ON or OFF but does not need to know how the motor inside the fan works.</p><h3>Why do we need Abstraction?</h3><p>Without Abstraction: User may get confused by unnecessary implementation details. Code becomes complex and difficult to understand.</p><p>With Abstraction: It focuses on <strong>what</strong> the system does, not <strong>how</strong> it does it. User can interact only with required interfaces.</p><h3>How Abstraction can be achieved?</h3><p>In Dart, Abstraction can be achieved in two ways:</p><p>3.1 Using Abstract classes (uses keyword extends)</p><p>Abstract class: Abstract class can have both abstract methods and concrete methods. Abstract classes cannot be instantiated because they may contain abstract methods without implementation.</p><p>If a class contains abstract methods, then the class must be abstract. If class is abstract, it may or may not contain abstract methods.</p><p>Abstract methods must be overridden in all the subclasses.</p><pre>abstract class Payment {<br>  void pay();   // abstract method<br>}<br><br>class UPI extends Payment {<br>  @override<br>  void pay() {<br>    print(&quot;Payment done using UPI&quot;);<br>  }<br>}<br><br>class CreditCard extends Payment {<br>  @override<br>  void pay() {<br>    print(&quot;Payment done using Credit Card&quot;);<br>  }<br>}<br><br>void main() {<br>  Payment p;<br><br>  p = UPI();<br>  p.pay();<br><br>  p = CreditCard();<br>  p.pay();<br>}</pre><p>Here, Payment declares only what to do; it does not define how payment happens<strong>. </strong>Implementation details are hidden from the user. This is called abstraction</p><p>3.2 Using Interfaces</p><p>In Dart every class can act as an interface and uses the <strong>implements</strong> keyword. It forces the client to implement all the methods that have been defined.</p><pre>// Interface<br>class Vehicle {<br>  void start() {}<br>}<br><br>// Implementing the interface<br>class Car implements Vehicle {<br>  @override<br>  void start() {<br>    print(&quot;Car started&quot;);<br>  }<br>}<br><br>class Bike implements Vehicle {<br>  @override<br>  void start() {<br>    print(&quot;Bike started&quot;);<br>  }<br>}<br><br>void main() {<br>  Vehicle v;<br><br>  v = Car();<br>  v.start();<br><br>  v = Bike();<br>  v.start();<br>}</pre><h3>Advantages</h3><p>Code reusability.</p><p>Easy to Maintain.</p><p>Reduces complexity (show only useful interfaces).</p><p>Enhances security.</p><h3>Disadvantages</h3><p>Abstraction can increase design complexity and code overhead.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b8c7bbf73666" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Polymorphism in Dart]]></title>
            <link>https://medium.com/@bgaasmitha/polymorphism-in-dart-7ed3b7a58689?source=rss-115943537542------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ed3b7a58689</guid>
            <category><![CDATA[polymorphism]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[oops-concepts]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[BG Aasmitha]]></dc:creator>
            <pubDate>Tue, 27 Jan 2026 05:34:23 GMT</pubDate>
            <atom:updated>2026-01-29T04:20:29.681Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. What is Polymorphism?</h3><p>It allows the same method name to behave differently based on the object that calls it. Polymorphism can be achieved through Inheritance.</p><h3>2. Why Polymorphism?</h3><p>Without Polymorphism: Need to write separate code logic for each subclass. Hard to maintain.</p><pre>class Dog {<br>  void sound() {<br>    print(&quot;Dog barks&quot;);<br>  }<br>}<br><br>class Cat {<br>  void sound() {<br>    print(&quot;Cat meows&quot;);<br>  }<br>}<br><br>void makeSound(Object animal) {<br>  if (animal is Dog) {<br>    animal.sound();<br>  } else if (animal is Cat) {<br>    animal.sound();<br>  }<br>}<br><br>void main() {<br>  Dog dog = Dog();<br>  Cat cat = Cat();<br><br>  makeSound(dog);<br>  makeSound(cat);<br>}</pre><p>With polymorphism: All subclasses are treated as the same parent type. Code becomes cleaner and more reusable.</p><pre>class Animal {<br>  void sound() {<br>    print(&quot;Animal makes a sound&quot;);<br>  }<br>}<br><br>class Dog extends Animal {<br>  @override<br>  void sound() {<br>    print(&quot;Dog barks&quot;);<br>  }<br>}<br><br>class Cat extends Animal {<br>  @override<br>  void sound() {<br>    print(&quot;Cat meows&quot;);<br>  }<br>}<br><br>void makeSound(Animal animal) {<br>  animal.sound(); // runtime polymorphism<br>}<br><br>void main() {<br>  Animal dog = Dog();<br>  Animal cat = Cat();<br><br>  makeSound(dog);<br>  makeSound(cat);<br>}</pre><h3>3. Types of Polymorphism</h3><p>Generally, there are two types of polymorphism:</p><ol><li>Method overriding (supported in Dart)</li><li>Method overloading (Dart does not support Method overloading, but similar behavior can be achieved through optional parameters).</li></ol><p>3.1 Method Overriding</p><p>A child class provides its own implementation of a method defined in the parent class.</p><p>Decided at runtime (which method to call).</p><pre>class Animal {<br>  void sound() {<br>    print(&quot;Animal makes a sound&quot;);<br>  }<br>}<br><br>class Dog extends Animal {<br>  @override<br>  void sound() {<br>    print(&quot;Dog barks&quot;);<br>  }<br>}<br><br>class Cat extends Animal {<br>  @override<br>  void sound() {<br>    print(&quot;Cat meows&quot;);<br>  }<br>}<br><br>void makeSound(Animal animal) {<br>  animal.sound(); // runtime polymorphism<br>}<br><br>void main() {<br>  Animal dog = Dog();<br>  Animal cat = Cat();<br><br>  makeSound(dog);<br>  makeSound(cat);<br>}</pre><p>3.2 Method Overloading</p><p><strong>Dart does not support method overloading</strong>, where the same method name has different parameter types or a different number of parameters. <strong>Similar behavior can be achieved using optional or named parameters.</strong></p><p>3.2.1 Optional parameters</p><pre>class Calculator {<br>  int add(int a, int b, [int? c]) {<br>    if (c != null) {<br>      return a + b + c;<br>    }<br>    return a + b;<br>  }<br>}<br><br>void main() {<br>  Calculator calc = Calculator();<br>  print(calc.add(2, 3));     // 5<br>  print(calc.add(2, 3, 4));  // 9<br>}</pre><p>3.2.2 Named Parameters</p><pre>class Calculator {<br>  int add({int a = 0, int b = 0, int c = 0}) {<br>    return a + b + c;<br>  }<br>}<br><br>void main() {<br>  Calculator calc = Calculator();<br><br>  print(calc.add(a: 2, b: 3));        // 5<br>  print(calc.add(a: 2, b: 3, c: 4));  // 9<br>  print(calc.add(a: 5));              // 5<br>}</pre><h3>4.Real-Time Example: <strong>Payment System</strong></h3><p>An application supports <strong>multiple payment methods</strong>: Credit Card, UPI, Cash</p><pre>abstract class Payment {<br>  void pay();<br>}<br><br>class CreditCard extends Payment {<br>  @override<br>  void pay() {<br>    print(&quot;Payment done using Credit Card&quot;);<br>  }<br>}<br><br>class UPI extends Payment {<br>  @override<br>  void pay() {<br>    print(&quot;Payment done using UPI&quot;);<br>  }<br>}<br><br>class Cash extends Payment {<br>  @override<br>  void pay() {<br>    print(&quot;Payment done using Cash&quot;);<br>  }<br>}<br><br>void processPayment(Payment payment) {<br>  payment.pay(); // runtime polymorphism<br>}<br><br>void main() {<br>  Payment p1 = CreditCard();<br>  Payment p2 = UPI();<br>  Payment p3 = Cash();<br><br>  processPayment(p1);<br>  processPayment(p2);<br>  processPayment(p3);<br>}</pre><p>All payments are processed using the <strong>same method</strong>, but the behavior changes based on the object.</p><h3>5.Advantages</h3><p>Code reusability: Same method works for many objects.</p><p>Cleaner and flexible code: No long if-else chains.</p><p>Improves maintainability.</p><p>Supports runtime decision-making.</p><h3>6.Disadvantages</h3><p>Debugging can be tricky.</p><p>Poor design can lead to misuse.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ed3b7a58689" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Inheritance in Dart]]></title>
            <link>https://medium.com/@bgaasmitha/inheritance-in-dart-dfc6bfea9d4e?source=rss-115943537542------2</link>
            <guid isPermaLink="false">https://medium.com/p/dfc6bfea9d4e</guid>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[programming-basic]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[oops-concepts]]></category>
            <dc:creator><![CDATA[BG Aasmitha]]></dc:creator>
            <pubDate>Fri, 23 Jan 2026 06:46:47 GMT</pubDate>
            <atom:updated>2026-01-29T04:16:32.051Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>What is Inheritance?</strong></h3><p>Mechanism where a child class <strong>reuses and extends</strong> fields &amp; methods of a parent class. Child classes <strong>cannot access private members</strong> (those starting with _) from a parent class if it is in a different file (library), but <strong>constructors can be called using super ()</strong>.</p><h3><strong>Why we need Inheritance?</strong></h3><p><strong>Without Inheritance</strong>: Code duplication everywhere and poor code organization.</p><pre>class LEDBulb {<br>  void turnOn() {<br>    print(&quot;Bulb is ON&quot;);<br>  }<br><br>  void turnOff() {<br>    print(&quot;Bulb is OFF&quot;);<br>  }<br>}<br><br>class CFLBulb {<br>  void turnOn() {<br>    print(&quot;Bulb is ON&quot;);<br>  }<br><br>  void turnOff() {<br>    print(&quot;Bulb is OFF&quot;);<br>  }<br>}</pre><p>In above example same turnOn(), turnOff() code repeated in both the classes. Poor code organization.</p><p><strong>With Inheritance</strong>: No code duplication (reason: common code in all the child classes organized in base class). Enhances reusability.</p><pre>class Bulb {<br>  void turnOn() {<br>    print(&quot;Bulb is ON&quot;);<br>  }<br><br>  void turnOff() {<br>    print(&quot;Bulb is OFF&quot;);<br>  }<br>}<br><br>class LEDBulb extends Bulb {<br>  void savePower() {<br>    print(&quot;LED saves power&quot;);<br>  }<br>}<br><br>class CFLBulb extends Bulb {<br>  void warmUp() {<br>    print(&quot;CFL warming up&quot;);<br>  }<br>}</pre><p>Here common code is organized in the base class. No code Duplication.</p><h3>Types of Inheritance</h3><ol><li><strong>Single Inheritance</strong> (one parent, one child) Child class Inherits the Properties from Parent class</li></ol><pre>// Parent class<br>class Bulb {<br>  int watt = 0;<br><br>  void switchOn() {<br>    print(&quot;Bulb is switched ON&quot;);<br>  }<br><br>  void switchOff() {<br>    print(&quot;Bulb is switched OFF&quot;);<br>  }<br>}<br><br>// Child class (Single Inheritance)<br>class LEDBulb extends Bulb {<br>  void showWatt() {<br>    print(&quot;Wattage: ${watt}W&quot;);<br>  }<br>}<br><br>void main() {<br>  LEDBulb led = LEDBulb();<br>  led.watt = 9;<br><br>  led.switchOn();   // inherited<br>  led.showWatt();   // own method<br>  led.switchOff();  // inherited<br>}</pre><p>2. <strong>Multilevel Inheritance</strong> (A → B→ C): Multilevel inheritance<strong> </strong>is when a class inherits from a class that is already inherited from another class.</p><p>class B inherits the properties from class A. class C inherits the properties from child B (therefore also gets everything from <strong>Class A</strong> (through B)).</p><pre>// Level 1<br>class Bulb {<br>  void switchOn() {<br>    print(&quot;Bulb is switched ON&quot;);<br>  }<br><br>  void switchOff() {<br>    print(&quot;Bulb is switched OFF&quot;);<br>  }<br>}<br><br>// Level 2<br>class LEDBulb extends Bulb {<br>  int watt = 12;<br><br>  void showWatt() {<br>    print(&quot;Wattage: ${watt}W&quot;);<br>  }<br>}<br><br>// Level 3<br>class SmartLEDBulb extends LEDBulb {<br>  void connectWifi() {<br>    print(&quot;Smart LED Bulb connected to WiFi&quot;);<br>  }<br>}<br><br>void main() {<br>  SmartLEDBulb smart = SmartLEDBulb();<br><br>  smart.switchOn();     // Bulb<br>  smart.showWatt();     // LEDBulb<br>  smart.connectWifi();  // SmartLEDBulb<br>  smart.switchOff();    // Bulb<br>}</pre><p>3. <strong>Hierarchical inheritance:</strong> Hierarchical inheritance is when multiple subclasses inherit from the same superclass.</p><pre>// Parent class<br>class Bulb {<br>  void switchOn() {<br>    print(&quot;Bulb is switched ON&quot;);<br>  }<br><br>  void switchOff() {<br>    print(&quot;Bulb is switched OFF&quot;);<br>  }<br>}<br><br>// Child class 1<br>class LEDBulb extends Bulb {<br>  void powerSave() {<br>    print(&quot;LED Bulb saves power&quot;);<br>  }<br>}<br><br>// Child class 2<br>class CFLBulb extends Bulb {<br>  void warmLight() {<br>    print(&quot;CFL Bulb gives warm light&quot;);<br>  }<br>}<br><br>void main() {<br>  LEDBulb led = LEDBulb();<br>  CFLBulb cfl = CFLBulb();<br><br>  led.switchOn();<br>  led.powerSave();<br>  led.switchOff();<br><br>  cfl.switchOn();<br>  cfl.warmLight();<br>  cfl.switchOff();<br>}</pre><p>4. <strong>Multiple Inheritance: </strong>A class gets properties and functions from multiple sources.</p><p>This can be achieved in two ways</p><p>4.1 Using Mixins</p><p>When you want to <strong>reuse code </strong>across multiple classes<strong> </strong>use mixins<strong>.</strong></p><pre>// Mixin 1<br>mixin PowerSaving {<br>  void savePower() {<br>    print(&quot;Power saving mode enabled&quot;);<br>  }<br>}<br><br>// Mixin 2<br>mixin WifiEnabled {<br>  void connectWifi() {<br>    print(&quot;Bulb connected to WiFi&quot;);<br>  }<br>}<br><br>// Base class<br>class Bulb {<br>  void switchOn() {<br>    print(&quot;Bulb is switched ON&quot;);<br>  }<br>}<br><br>// Child class using multiple inheritance<br>class SmartBulb extends Bulb with PowerSaving, WifiEnabled {<br>  void features() {<br>    print(&quot;Smart Bulb features activated&quot;);<br>  }<br>}<br><br>void main() {<br>  SmartBulb bulb = SmartBulb();<br><br>  bulb.switchOn();     // from Bulb<br>  bulb.savePower();    // from PowerSaving mixin<br>  bulb.connectWifi();  // from WifiEnabled mixin<br>  bulb.features();     // own<br>}</pre><p>4.2 Using Interface: When you want to <strong>define rules </strong>use interface. It forces the client to implement all the methods that are defined in interface.</p><pre>// Interface 1<br>abstract class Switchable {<br>  void switchOn();<br>}<br><br>// Interface 2<br>abstract class WifiConnectable {<br>  void connectWifi();<br>}<br><br>// Class implementing multiple interfaces<br>class SmartBulb implements Switchable, WifiConnectable {<br><br>  @override<br>  void switchOn() {<br>    print(&quot;Smart Bulb is switched ON&quot;);<br>  }<br><br>  @override<br>  void connectWifi() {<br>    print(&quot;Smart Bulb connected to WiFi&quot;);<br>  }<br>}<br><br>void main() {<br>  SmartBulb bulb = SmartBulb();<br><br>  bulb.switchOn();<br>  bulb.connectWifi();<br>}</pre><h3>Advantages</h3><ol><li>Code reuse.</li><li>Easy to maintain.</li><li>Proper code organization.</li><li>Supports polymorphism.</li></ol><h3>Disadvantages</h3><ol><li>Tight coupling between parent and child.</li><li>Less flexibility compared to composition.</li></ol><h3>Best Practices</h3><ol><li>Avoid deep hierarchies.</li><li>Use inheritance only for is-a relationship.</li><li>Prefer <strong>composition over inheritance </strong>(inheritance-&gt;tightly coupled, composition-&gt;loosely coupled)<strong>.</strong></li><li>Inheritance is not allowed when a class is final or sealed.</li></ol><h3>Conclusion</h3><p>Inheritance helps organize and reuse code efficiently, but it should be used carefully. In Dart, developers are encouraged to prefer composition and mixins over deep inheritance hierarchies for better flexibility and maintainability.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dfc6bfea9d4e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>