DEV Community

Cover image for Abstract Classes and Abstract Methods in Dart
Jay Tillu😎
Jay Tillu😎

Posted on • Updated on

Abstract Classes and Abstract Methods in Dart

  • An abstract class is a type of class that cannot be instantiated directly which means an object cannot be created from it.

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

  • To define an abstract class we use abstract keyword.

The syntax for defining Abstract Class

abstract class class_name {
  // Body of abstract class
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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 Code

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");
  }
}
main(){
  Jay jay = new Jay();
jay.talk();
  jay.walk();
}

Output
Jay can talk
Jay can walk
Enter fullscreen mode Exit fullscreen mode

Difference between Abstract class and Interface


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

So, guys, that’s it for abstract classes. Feel free to tell me if I miss something, I’ll 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 hesitate 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.

For More Information Please Visit Following Links

Jai Hind, Vande Mataram 🇮🇳

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊

Top comments (0)