Inheritance in Dart

Rushali Sarkar
6 min readNov 9, 2021

Mixins, Override, and Inheritance in Dart

Inheritance

Inheritance is a wonderful feature of Object Oriented Programming giving us a huge scope of code reusability. Just like we inherit features from our ancestors adding our own uniqueness to it, in a very similar way classes can also inherit from other classes to make their code more compact and readable. Let’s understand this with an example,

Suppose, there is a class Add, which has one function that is, addNumbers which adds two numbers and return the result,

The output of this code piece would be just 9, with nothing fancy in it. But let’s say we have another class subtract, which has a function subtractNumbers, which basically just subtracts two numbers and returns the result.

This is also a pretty simple class that simply subtracts two numbers, and the result of this code piece would be 4. All is good so far. But, let’s say we also want our Subtract class to be able to add numbers in case a user requests it to.

Note: (It’s definitely absurd for a subtract class to add numbers, and I, with all means, support good readable code and do not recommend such stupidity, but just for the sake of this topic, let’s consider this. We are soon gonna fix it ahead in the blog, so keep reading!)

One way to do that would be to write a function in our subtract class to add numbers and which is a good way to approach it, but we are good coders with good coding ethics, who do not support code repetition, and let’s assume we already have the “class Add” available to us and by no means can we delete it. And since we are good programmers, we believe in reusing codes as much as possible. So, what to do now? Here, comes the magic of inheritance. We can inherit the add function from our Add class and use it as an instance of Subtract class without having to rewrite the add function all over again. Let’s look at some code,

As is evident from the code above, we have used the “extend” keyword, to inherit from Add class to Subtract class and without us having to write down the addNumbers function the addNumbers function is automatically made available to our Subtract class. Now, there is another challenge in front of us. What if we want the addNumbers function in our Subtract class but with some modifications in it. Let’s say we want our addNumbers function to print an extra line “Function accessed from subtract class through inheritance” when it is called as an instance of Subtract class. This is where “override” comes into the picture.

Override

The override feature allows us to override the methods present in the superclass (The class which is inherited, for this case it is the class Add) into the subclass (The class which inherits, for this case it is the class Subtract). Let's view some code,

As we see, the function addNumbers is being overridden in our subclass and provides whatever we want it to do. But, here only the new things get added as we see only the print statement in our subclass is being executed and nothing in our superclass has been executed. What if we want to keep all of the functionality of the superclass and just add something new to our subclass? Don’t worry, we have a way out even here. We can use the super keyword to do so.

Now, we can see that both the print statements in the superclass and the subclass are printed and the override feature has provided us with this extra ability to add new functionality to inherited functions.

Mixins

So far so good, but let’s now take the challenge a little further. Let’s say we want to design a calculator class. A Calculator class would have all functions, not just add and subtract but also operations like multiply and divide and many others. (Let’s just talk about basic operations now).

Someone might say now we can inherit all functions from the respective classes and add them to our main “Calculator” class. But, the problem with it is dart does not support inheritance from multiple classes, but in hierarchies. As I made a note before, We are good programmers and we do not write meaningless codes, where subtract class adds numbers or multiply class would divide numbers. Hierarchial inheritance is a way out but not a “very good” way out. This is where the concept of “mixins” is very useful.

The Wikipedia definition of mixins is pretty expressive and speaks for itself,

“In object-oriented programming languages, a Mixin is a class that contains methods for use by other classes without having to be the parent class of those other classes.”

We can use methods from various other classes, without bringing the concept of “superclass, subclass” into the picture.

So, this solves our problem! Now we can use functions from multiple classes without having to worry about the “single inheritance” bindings of dart.

Inheritance or Mixin?

This can be a question of controversy with no absolute answer. Both inheritance and mixins have their own pros and cons. The biggest failure of inheritance in dart is, it does not allow multiple inheritance which often does not make sense, and makes it difficult for coders. This is where “mixins” comes as a savior. But, then again the class which you want to use as a mixin cannot have a constructor of its own.

So, there is no definite answer. Both of them are useful and are to be used according to the use case.

Thank you, all for making it to the end! I have used, Carbon for these beautiful code-editor-like images. This was suggested to me by Riya Jain (forever thankful to her for showing me this awesome website.)

--

--