Abstraction is the concept of object-oriented programming that “shows” only essential attributes and “hides” unnecessary information. The main purpose of abstraction is hiding the unnecessary details from the users.

In Dart, we can do the abstraction. In simple words, abstraction means – something happened but everyone does not know. Let’s say, is we switch on fan, then fan start on, but we don’t need to know the electricity wiring of the fan.

We can implement abstraction using either ‘abstract class’ or ‘interfaces’.

Abstract class:

Properties of abstract class ->

  • An instance of an abstract class cannot be created.
  • Constructors are allowed.
  • We can have an abstract class without any abstract method.
  • There can be a final method in abstract class but any abstract method in class(abstract class) cannot be declared as final  or in simpler terms final method cannot be abstract itself as it will yield an error: “Illegal combination of modifiers: abstract and final”.

Let’s understand below, usage of abstract class and abstract method …

Code 1:

// abstract class -> ‘car’
abstract class car{
 /**abstract methids are
  * engine() & modelNumber()
  */
  void engine(){}
  void modelNumber(){}
}
class lamborgini extends car{
String name=”Lam56tF”;
 
  engine(){
    print(‘lamborgini engine is super & secret !!’);
  }
  modelNumber(){
    print(‘model number is $name’);
  }
}
void main(){
  var gallardo = lamborgini();
  gallardo.engine();
  gallardo.modelNumber();
}
output

Interfaces

Before starting the concept of interfaces let’s see why we need to use this.

  • It is used to achieve total abstraction.
  • Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances.
  • It is also used to achieve loose coupling.

Dart has no interface keyword. Instead, all classes implicitly define an interface. Therefore, you can implement any class.

Implicit interfaces

Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. If you want to create a class A that supports class B’s API without inheriting B’s implementation, class A should implement the B interface.

A class implements one or more interfaces by declaring them in an implements clause and then providing the APIs required by the interfaces. For example:

Code 2:

class hacker{
  void type(){
    print(‘there are 3 type of hackers!’);
  }
}
class redhats implements hacker{
  type(){
    print(‘redhats destroys blackhats!’);
  }
}
void main(){
  var hack;
  hack=redhats();
  hack.type();
 
}
output
Let’s see another example…

Code 3:
// A person. The implicit interface contains greet().
class Person {
  // In the interface, but visible only in this library.
  final String _name;
 
  // Not in the interface, since this is a constructor.
  Person(this._name);
 
  // In the interface.
  String greet(String who) => ‘Hello, $who. I am $_name.’;
}
 
// An implementation of the Person interface.
class Impostor implements Person {
  String get _name => ”;
 
  String greet(String who) => ‘Hi $who. Do you know who I am?’;
}
 
String greetBob(Person person) => person.greet(‘Bob’);
 
void main() {
  print(greetBob(Person(‘Kathy’)));
  print(greetBob(Impostor()));
}
output

Read more: https://dart.dev/samples#interfaces-and-abstract-classes