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
}
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 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
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.
Learn more about Dart and Flutter
- List in Dart
- Abstract class and Abstract Methods in Dart
- Interface in Dart
- Constructors in Dart
- Arrow Function in Dart
- User Defined Function in Dart
- Functions in Dart
- Switch case in Dart
- Conditionals in Dart
- Operators in Dart
- Keywords in Dart
- Variables in Dart
- Data Types in Dart
Top comments (0)