Get Started With OOPS In Flutter 😎

Introduction

Hey folks, I hope you are doing well. In the previous article, we already learned about classes. This section will discuss Inheritance, Super Constructor, and some features in dart Language. So let’s get started. Please see my previous article if you have any doubts about classes or other features like functions.

Inheritance 🤩

Inheritance in dart is defined as the process in which one class derives another class's properties and characteristics. It is helpful as it provides an ability with which we can create a new class from an existing class. Don't worry about the base class, or derived class I will explain more please see the below image. Simply inheritance is the relationship between two classes.

So, the above picture clearly shows what inheritance actually is. Here it's working with the above values like person class and bank details class.

void main() {
    final bank = Bank(name: 'Navinprakash', address: 'TamilNadu,India', phone: 1234567899, accountType: 'Savings Account', accountBalance: 10000.00, bankName: 'State Bank of India', bankAddress: 'India');
    print(bank.name);
    print(bank.address);
    print(bank.phone);
    print(bank.accountType);
    print(bank.accountBalance);
    print(bank.bankName);
    print(bank.bankAddress);
}
class Person {
    Person({
        required this.name,
        required this.address,
        required this.phone
    });
    final String name;
    final String address;
    final int phone;
}
class Bank extends Person {
    Bank({
        required String name,
        required String address,
        required int phone,
        required this.accountType,
        required this.accountBalance,
        required this.bankName,
        required this.bankAddress
    }): super(name: name, address: address, phone: phone);
    final String accountType;
    final double accountBalance;
    final String bankName;
    final String bankAddress;
}

Super Constructor

In the above piece of code, you will see the new keyword in the bank class’s constructor that’s called a super constructor. The child class inherits all of the main class but can’t inherit the constructor of the main class, so we are using the super constructor to inherit the main class constructor.

Abstract Class

An Abstract class is a class that defines some methods and properties without any implementations. Use the keyword “abstract” for defining an abstract class. An abstract class can’t be instantiated. An abstract class defined interfaces.

void main() {
    final square = Square(side: 10.0);
    print(square.area());
}
//Note area does not have a body only declared method.
abstract class Shape {
    double area();
}
class Square implements Shape {
    Square({
        required this.side
    });
    final double side;
    double area() => side * side;
}

Abstract Class with functions

void main() {
    final square = Square(side: 10.0);
    displayArea(square);
}
//Adding Extra functions to display square vaule it helps write better code.
void displayArea(Shape shape) {
    print(shape.area());
}
//Note area not has body only declared method.
abstract class Shape {
    double area();
}
class Square implements Shape {
    Square({
        required this.side
    });
    final double side;
    double area() => side * side;
}

Computed Properties

Computed properties set getters and setters in dart language. Please see the below the code snippet, I use the get keyword instead of using parentheses. I covered only getters, please learn more in official dart documentation for setters.

void main() {
    final square = Square(side: 10.0);
    displayArea(square);
}
//Adding Extra functions to display square vaule it helps write better code.
void displayArea(Shape shape) {
    print(shape.area);
}
//using computed properties "get".
abstract class Shape {
    double get area;
}
class Square implements Shape {
    Square({
        required this.side
    });
    final double side;
    double get area => side * side;
}

Conclusion

I hope you are learning something about dart inheritance and abstract classes. This is the basic overview about inheritance and abstract classes but I highly recommend dart classes in official documentation so you learn more interesting features in the documentation. In the next article, we will learn about mixins, lists, and maps. Thank you.


Similar Articles