Table of contents
1.
Introduction
2.
Dart Abstract Classes
2.1.
Abstract Class Declaration 
2.2.
Example
3.
Multiple Derived Classes
4.
Frequently Asked Questions
5.
Conclusion 
Last Updated: Mar 27, 2024

Dart Abstract Classes

Author Pradeep Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will look into Dart Abstract Class. We'll look at how an abstract class is defined and how derived classes can override its properties. We can't construct an object out of an abstract class, but we can make one out of a non-abstract class that is derived from it.

If you are new to Dart and are not familiar with classes in Dart, you can check out this article.

Dart Abstract Classes

To state-certain fundamental properties, an abstract class is utilized. In Dart, you can't instantiate an abstract class. Making an abstract class object is not possible; attempting to do so would result in a compilation error.

Abstract Class Declaration 

In Dart, the abstract keyword is used to create an abstract class.

abstract class className{
    //body of the class
}

Example

We'll explore how to declare an abstract class and how to create a derived class from an abstract class in this example.

// Creating an abstract class
abstract class GradeSheet {
  // abstract method
  void total_marks();
}

// derived class
class Marks extends GradeSheet {
  // class variables
  var maths;
  var english;

  // method to change marks of maths subject
  void setMathsMarks(var num) {
    maths = num;
  }

  // method to change marks of english subject
  void setEnglishMarks(var num) {
    english = num;
  }

  // overriding the total_marks function to calculate the total
  @override
  void total_marks() {
    var total = maths + english;
    print("Total Marks are: " + total.toString());
  }
}

main() {
  // Making an object of marks class
  Marks m1 = new Marks();
  // setting marks of english
  m1.setEnglishMarks(30);
  // setting marks of maths
  m1.setMathsMarks(50);
  // printing the total marks
  m1.total_marks();
}

Output:

Total Marks are: 80

Multiple Derived Classes

Multiple classes can inherit from the same abstract class, i.e., one abstract class can have multiple derived classes. An abstract class's abstract method or variable can be overridden in all of its derived classes. We override the run function in three derived classes of the Animal abstract class in this program.

// Creating an abstract class
abstract class Animal{
   // abstract method
   void run();
}
 
// Derived Class
class Cat extends Animal{
    @override
    void run(){
        print("A cat is running");
    }
}
// Derived Class
class Dog extends Animal{
    @override
    void run(){
        print("A dog is running");
    }
}
// Derived Class
class Horse extends Animal{
    @override
    void run(){
        print("A horse is running");
    }
}
 
main(){
    // Creating an object of Cat Class
    Cat cat= new Cat();
    // Calling the run function of the cat object
    cat.run();
    // Creating an object of Dog Class
    Dog dog= new Dog();
    // Calling the run function of the dog object
    dog.run();
    // Creating an object of Horse Class
    Horse horse= new Horse();
    // Calling the run function of the horse object
    horse.run();
}

Output:

A cat is running
A dog is running
A horse is running

Frequently Asked Questions

What is the definition of an abstract method?
An abstract method lacks a body and so cannot be declared within an abstract class. It is defined within the same abstract class's derived class.

 

How is an interface distinct from an abstract class?
A state can't be saved in an interface. They are allowed to have properties, but only abstract properties.

 

Are all methods, in an abstract class, of abstract nature?
An abstract class can have or does not have abstract methods, but it must be an abstract class if it has an abstract method.

Conclusion 

In this article, we have extensively discussed the Abstract ClassWe hope that this blog has helped you enhance your knowledge regarding the Dart Abstract Classes, and to learn more, check out our other article on Dart InterfacesAnd also check out the awesome content on the Coding Ninjas Website.
Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test Series

Recommended Readings:

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass