Object Oriented Programming in Dart: Inheritance
What is Inheritance in Dart? What are the types of Inheritance? What are the `extends` and `with` keywords and the differences between them?

Search for a command to run...
What is Inheritance in Dart? What are the types of Inheritance? What are the `extends` and `with` keywords and the differences between them?

No comments yet. Be the first to comment.
This series covers all the important concepts of OOPs in Dart i.e: Classes, Objects, Constructor, Abstraction, Inheritance, Polymorphism, and Encapsulation.
What is Abstraction in Dart? What is Abstract Class and Method? What should you keep in mind when using Abstract Class? When should you use Abstraction?
Lessons from Code, Content, and Consistency

Reliability basics: Outbox pattern + why it matters

Introducing Kafka/Redpanda + move to event-driven workflow

Connect Orders β Inventory (first working service-to-service flow)

Inventory service + gRPC + proto contracts

Where I described in depth what abstraction is and how it works using two simple examples.
Following that, we talked through Abstract Classes and Abstract Methods.
We also saw four rules/important factors to remember when using Abstraction. Then we discovered what Implicit Interface is in Dart. Finally, we looked at several Abstraction use scenarios.
All right, so in this article, we'll talk about Inheritance. Let's get started without further ado.
class Shape {
String? color;
void giveSides({required int noOfSide}){
print('$noOfSide Sides with $color Color');
}
}
class Triangle {}
extends Keywordclass Triangle extends Shape {}
class Shape {
String? color;
void giveSides({required int noOfSide}){
print('$noOfSide Sides with $color Color');
}
}
class Triangle extends Shape{}
void main() {
final triangle = Triangle();
triangle.color = 'Red';
triangle.giveSides(noOfSide: 3);
}
Console :
'3 Sides with Red Color'
color and method giveSides is not defined in the Triangle class, you are still able to use these. This is called Inheritance. You are inheriting all the stuff.
class Parent {}
class Child extends Parent {}

class A {}
class B extends A {}
class C extends B {} // and so on...
class Shape {
String? color;
}
class Triangle extends Shape {
void area (int l, int h){
print(1/2*l*h);
}
}
class EquilateralTriangle extends Triangle {
// A triangle with equal sides
}

class Shape {
String? color;
void giveSides({required int noOfSide}){
print('$noOfSide Sides with $color Color');
}
}
class Triangle extends Shape {
void area (int l, int h){
print(1/2*l*h);
}
}
class Square extends Shape {
void area (int s){
print(s*s);
}
}
class Circle extends Shape {
void area (int radius){
print(3.14*radius*radius);
}
}
/* Shape
|
.---------.---------.
Triangle Square Circle
*/
class A {}
class B {}
class C extends A,B{} // β
mixinsImportant points:
- Mixins Can't have Constructor
- You cannot use mixin if the parent class is extending some other class.
//* Point 1
mixins A {
A(); // β
}
//* Point 2
class A {}
class B extends A {}
class C with B {} // β Because B is extending some other class
mixins?with keyword as shown below :class A {}
class B {}
class C with A,B {}

class Shape {
String? color;
}
class Triangle {
void area (int l, int h){
print(1/2*l*h);
}
}
class EquilateralTriangle with Shape, Triangle {
// A triangle with equal sides
}
void main() {
final equiTriangle = EquilateralTriangle();
equiTriangle.color = 'Red';
equiTriangle.area(1,2);
}
extends, with and implements| extends | with | implements |
|---|---|---|
| Can inherit only one class | Can inherit multiple classes | Can inherit multiple classes |
| Not require to implement all the members | Not require to implement all the members | It Requires to implement all the memebers |
| Use this when you need to create a more specific version of a class. For instance, if Apple extends from the Fruit class (Example we saw in the previous article), it means all the properties, variables, and functions defined in the Fruit class will be available in the Apple class. | Use this when you want to extend multiple classes and also don't want to forcefully implement that classes' methods and properties | Use this when you want the class which inherits the type of the parent class. This means when you implement a class you are basically saying the MyClass looks like OtherClass. And you need to redefine all the methods and properties of that OtherClass |
