Abstract class in Flutter, E-Com App 8

In Dart and Flutter we can extend a class. That’s inheritance. However, we can also extend an abstract class in Flutter.

The concept of abstract class plays an important role in Flutter app development. Just like the “mixin”.

We’ve seen the difference between mixin and extend in Flutter in the previous section.

As a result, we have learned that the mixin keyword plays an important role in Dart, as well as in Flutter. However, the extend keyword is quite common. Because we’re more or less familiar with the concept of inheritance. 

We know that a child class can extend all the attributes of a parent class. For that reason we see a Widget tree in Flutter. 

That part is okay.

But what is the role of abstract class in Flutter? Moreover, when we have “mixin” and “extend”, why do we need an abstract class?

Let’s try to understand with the following code.

abstract class Intelligence {
  void think() {
    print('Think');
  }
}

abstract class IntelligentMachine extends Intelligence {
  void work() {
    print('Working');
  }

  void walk() {
    print('Walking');
  }
}

class Robot extends IntelligentMachine {
  void doEveryThing() {
    think();
    work();
    walk();
    print('I can think but wait for instruction, then work, and walk.');
  }
}

abstract class AI {  
  // Duplicate method
  void think() {
    print('Think');
  }
  void takeDecision() {
    print('Then take Decisions.');
  }
}

class Computer extends AI {
  void instructRobot() {
    think();
    takeDecision();
    print('It instructs the robot.');
  }
}
  

main() {
  Robot robu = Robot();
  robu.doEveryThing();
  Computer compu = Computer();
  compu.instructRobot();  
}

Let’s run the code. It gives an output as follows.

Think
Working
Walking
I can think but wait for instruction, then work, and walk.
Think
Then take Decisions.
It instructs the robot.

However there is a problem in the above code.

We have a Robot object that can think but wait for the instructions. As a result, we need an extra abstract class that should have the “think()” method. Right? 

Unfortunately, the Computer object also needs the “think()” method, because it instructs the Robot.

As a result we need another abstract class that has the “think()” method. 

However, it duplicates the code. 

How do we overcome this problem?

The answer is mixin. 

Therefore let’s change the above code and write it again as follows.

mixin Intelligence {
  void think() {
    print('Think');
  }
}

abstract class IntelligentMachine with Intelligence {
  void work() {
    print('Working');
  }

  void walk() {
    print('Walking');
  }
}

class Robot extends IntelligentMachine {
  void doEveryThing() {
    think();
    work();
    walk();
    print('I can think but wait for instruction, then work, and walk.');
  }
}

abstract class AI with Intelligence {    
  void takeDecision() {
    print('Then take Decisions.');
  }
}

class Computer extends AI {
  void instructRobot() {
    think();
    takeDecision();
    print('It instructs the robot.');
  }
}
  

main() {
  Robot robu = Robot();
  robu.doEveryThing();
  Computer compu = Computer();
  compu.instructRobot();  
}

As a result, we have avoided the code duplication. Let’s take a look at the output.

Think
Working
Walking
I can think but wait for instruction, then work, and walk.
Think
Then take Decisions.
It instructs the robot.

Code restriction with mixin

Mixin has another advantage that we can use.

Suppose we want only one class and that class should use any mixin. 

For example, in the above code, we want that only the Human object can think. As a result any other Computer, or Robot object cannot use the “think()” method.

In that case, we will write the above code in the following way.

mixin Intelligence on Human {
  void think() {
    print('Thinking');
  }
}

abstract class Machine {
  void work() {
    print('Working');
  }

  void walk() {
    print('Walking');
  }
}

class Robot extends Machine {
  void doEveryThing() {
    //think();
    work();
    walk();
    print('I cannot think, only work, walk.');
  }
}

// we don't need this anymore
abstract class AI {
  // Duplicate method
  void think() {
    print('Thinking');
  }
}

class Computer {
  void doIntelligentThing() {
    //think();
    print('It cannot take its own decisions.');
  }
}

abstract class Human {
  void canThinkOnly() {
    print('Human can think only.');
  }
}

class Person extends Human with Intelligence {}
  

main() {
  Robot robu = Robot();
  robu.doEveryThing();
  Computer compu = Computer();
  compu.doIntelligentThing();  
  Person person = Person();
  person.canThinkOnly();
}

Please watch the highlighted part. That will explain everything. 

Now, the mixin Intelligence only acts on Human objects.

As an outcome, no other objects can use it. If any object tries to use the same mixin, we will get errors.

Now we can take a look at the output.

Working
Walking
I cannot think, only work, walk.
It cannot take its own decisions.
Human can think only.

Certainly we can overcome the problem of multiple inheritance using mixin. Abstract class cannot also solve the code duplication.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

Python and Data Science

Twitter

Comments

4 responses to “Abstract class in Flutter, E-Com App 8”

  1. […] Secondly we will know how we can use a nested provider to enhance the performance of our E-Commerce app? […]

  2. […] before we have learned how we can use a nested provider to enhance the performance of our E-Commerce app. […]

  3. […] if we insert order into our products class, it will be cumbersome to handle the process that relates to order […]

  4. […] Because, we haven’t  inserted orders into our products class.  […]

Leave a Reply