Abstraction is one of the four main principles of objected oriented programming, as we discussed in the first part. Abstraction means in simple terms to hide unnecessary details.
An example would be an application; On the back-end, users don't care about what server the application is running on, how much space is being allocated(space being used) or how many api requests are being made daily. Most users only care if the application is working correctly, and there are no errors on the front-end.
In the example below, to initialize abstraction in dart; create an "abstract class" with the name of the class you want to create. And add some abstract methods, void methods, that don't return a value.
abstract class Author{
void books(){}
void awards(){}
}
In the example below we create a subclass then extend it. In, abstraction, a subclass or child class extending an abstract class is also required to have methods from the parent class or else an error will occur.
abstract class Author{
void books();
}
class JKRowling extends Author{
// void drive(){
// print("driving");
// }
}
void main(){
print(JKRowling);
}
output:
Error: The non-abstract class 'JKRowling' is missing implementations for
these members
If we uncomment the method in the JKRowling's class derived from the Author's class. Then, create a new instance of the JKRowling's class; We will see that our output no longer gives us an error.
abstract class Author{
void books();
}
class JKRowling extends Author{
void books(){
print("harry potter");
}
}
void main(){
print(JKRowling);
}
But, the example above will not output anything because the books method in the parent class is null. In order to see
bookswritten1.books()
value; The parent class, Author's books method has to be overridden. It is also called method overriding. Method overriding lets you modify or update a specific implementation of a method from a parent's class. Every subclass that uses the abstract class needs to have an override method.
abstract class Author{
void books();
}
class JKRowling extends Author{
@override
void books(){
print("harry potter");
}
}
void main(){
JKRowling booksWritten1 = new JKRowling();
booksWritten1.books();
}
output
harry potter
Now we see what we were expecting.
Abstract classes also can't create new instances of themselves. Look in the example below..
abstract class Author{
void books();
}
class JKRowling extends Author{
@override
void books(){
print("harry potter");
}
}
void main(){
Author author1 = new Author();
}
output:
Error: The class 'Author' is abstract and can't be instantiated.
SIDE NOTE: Abstract classes can't extend or use multiple abstract classes. If you run the code below, it will spit out an error. If we wanted to use multiple abstract classes's functionalities we would have to look to another type of abstraction called interfaces.
abstract class Author{
void books();
}
abstract class Movies{
void title();
}
class JKRowling extends Author, Movies{
@override
void books(){
print("harry potter");
}
}
void main(){
JKRowling booksWritten1 = new JKRowling();
booksWritten1.books();
}
Interfaces is similar to abstractions but the main difference, is that interfaces can extend or "implement" multiple abstract classes. In dart, there aren't any interfaces. So, here we create our own interface using abstract classes. In other programming languages, "interface class_name" is used.
abstract class Author{
void books();
}
abstract class Movies{
void title();
}
class JKRowling implements Author, Movies{
@override
void books(){
print("harry potter");
}
@override
void title(){
print("fantastic beasts and where to find them");
}
}
void main(){
JKRowling booksWritten1 = new JKRowling();
booksWritten1.books();
booksWritten1.title();
}
output:
harry potter
fantastic beasts and where to find them
In the example above, we can see that how interfaces can be useful to us.
This is was an introduction in knowing what abstraction is, how is it works, what purpose abstraction serves and ways that it can be beneficial.
Top comments (1)
can you give us alot of examples about why we use abstraction classes and thank you for this useful article