Add Your Heading Text Here

Abstraction is the process of providing the functionality or essential requirements and hiding the implementation details from users to reduce the complexity and effort.

The best real life example of Abstraction is ATM Machine. We perform many transaction operations in ATM Machine like money transfer, withdrawal, balance check etc. But, we doesn’t know the internal functionality of the ATM while we perform our different ATM transactions.

Abstraction in Dart can be achieved using Abstract Class.

before learning about abstract class it’s better to have a look at abstract methods.

Abstract Method

Methods declared without implementation and ends with semicolon are known as abstract methods.

They can be declared inside Abstract class only.

Any abstract methods declared inside particular class must be implemented or say overriden by the class which inherits that particular class.

Abstract Class

Abstract class must be declared using Abstract keyword.

They are useless until any concrete class (the class which is not abstract) inherits it.

If a class inherits the Abstract class then either it must implement all the methods of that abstract class or must be declared as abstract class.

Abstract class can have abstract methods as well as concrete methods(non-abstract methods).

Abstract class can not be instantiated as it may have abstract methods.

As abstract methods have no body. So, accessing abstract methods is useless as it isn’t have any implementation.

That’s why Abstract class is also refered as Incomplete class.

For ex :

If a car has not proper brakes working. Nobody will drive that unsafe and incomplete car to avoid accidents.

Similarly, Abstract keyword ensures that no one would be allowed to instantiate the instance of an Abstract class(incomplete class). Even if an Abstract class have only non-abstract methods.

Let’s understand the Abstraction with a more common Example .

Let say we want to create a program for the Animal Feed.

For Animal Feed program, we will need feedAnimal() method for each Animal.

So, it’s a better way to create an abstract method in an Abstract class. By making this method abstract we ensures that each class has a feed method.

For ex :

abstract class Animal {

// Abstract method feedback() with no body and ends with semicolon
  feedAnimal();

  sleepAnimal(){
    print("Sleeping...");
  }
}
class Cat extends Animal {
	feedAnimal(){
    print("Feed Cat") ;
  }
}
class Dog extends Animal {
	feedAnimal(){
    print("Feed Dog") ;
  }
}
class Fox extends Animal {
	feedAnimal(){
    print("Feed Fox") ;
  }
}
void main(){
	Animal c=new Cat();
	Animal d=new Dog();
	Animal f =new Fox();
	c.feedAnimal();
	d.feedAnimal();
	f.feedAnimal();
}

Output :

Feed Cat
Feed Dog
Feed Fox

For this program, we have created a super abstract class Animal with abstract method feedAnimal().

Now, each subclass of Animal class must have to implement the abstract method feedAnimal() of the Abstract class Animal.

This way we ensure that all the classes who are the subclasses of the Animal class must have the feedAnimal() method.

So Abstraction is defining characteristics for Animal class that each Animal must be feeded. How each Animal is feeded is implemented in the classes which inherits Animal class and hidden from the user.

So, Abstraction is the process of showing the essential details and hiding internal implementation process.

You can have upper hand on Abstraction by practicing it more and more.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons