Abstract Classes and Abstract Methods in Dart

Abstract Classes and Abstract Methods in Dart

In Dart, the abstract class is a type of class that cannot be instantiated directly which means an object cannot be created from it.

·

4 min read

  • An abstract class cannot be instantiated but it can be sub-classed.

  • To define an abstract class we use abstract keyword.

Syntax for defining Abstract Class

abstract class class_name {
  // Body of abstract class
}

Abstract Methods

Abstract methods can only exist within an abstract class. To make a method abstract, use a semicolon (;) instead of the method body.

void talk (); // Abstract method
void walk (); // Abstract method
  • Normal classes can extend the abstract class, but they have to override every abstract method.

  • You can also create normal methods in the abstract class. And to override normal method is not mandatory.

  • The abstract class will only complain when you don’t override the abstract method.

Sample Example


abstract class Person{
void walk();  //Abstract Method
  void talk();  //Abstract Method
}
class Jay extends Person{
  @override
  void walk() {
    print("Jay can walk");
  }
@override
  void talk() {
    print("Jay can talk");
  }
}

void main(){
  Jay jay = new Jay();
  jay.talk();
  jay.walk();
}

Output
Jay can talk
Jay can walk

Difference between Abstract class and Interface

So now after seeing both abstract class and interface. You might ask that technically they look the same. Yeah, that’s true that they are closely related. But guys they are not exactly the same. Let’s understand the basic difference between them.

Abstract ClassInterface
Abstract Class cannot be instantiated.As Normal Class acts as Interface technically it can be instantiated.
Abstract Methods can be created here.Abstract Methods cannot created here.
When the Abstract Class is extended by another class, only the Abstract method needs to override.When the Interface is implemented in another class every method and instance variable needs to override.
We can only extend one Abstract Class.We can implement multiple interfaces.

Conclusion

In summary, abstract classes in Dart provide a way to define common behaviour and enforce method implementations in sub-classes without allowing direct instantiation. Interfaces, on the other hand, are implemented by classes and require the implementation of all their methods and variables.

  • Abstract Classes cannot be used to create objects directly. They serve as blueprints for other classes to inherit from. Abstract classes can have both abstract methods (methods with no code) and regular methods.

  • Abstract Methods are special methods found only in abstract classes. They lack a method body and serve as placeholders. When a class extends an abstract class, it must provide implementations for all the abstract methods in that class.

  • Interfaces are similar to abstract classes but with some differences. While abstract classes can't be directly instantiated, interfaces can be. Abstract classes can have abstract methods, while interfaces require all methods to be fully defined in the implementing class. A class can implement multiple interfaces, but it can only extend one abstract class.

In essence, abstract classes are used to create reusable blueprints for classes, while interfaces ensure that specific methods are defined in implementing classes.

So, guys, that’s it for abstract classes. Feel free to tell me if I miss something, I’d love to learn it from you. Till then Keep Loving, Keep Coding. And Just like always I’ll catch you up in the next article.

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitant that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

Learn More about Dart and Flutter

Follow me for more such content.

Did you find this article valuable?

Support Jay Tillu by becoming a sponsor. Any amount is appreciated!