- S - Single Responsibility (It's very simple this is why didn't write any example for it)
- O - Open/Close
- L - Liskov Substitute
- I - Interface segregation
- D - Dependency Inversion
These are set of principles that are widely applicable in OOP. Since, flutter is powered with dart so it means these can play very vital role in writing efficient code, maintaining and keeping the code clean
Not going to write them in depth, you can use google/GPT. I've mentioned few key points that causes few confusion when it comes to dart specifically.
abstractkeyword is use to define classes for which you need to write abstract methods and to avail partial data hiding concept.interfacekeyword is use to achieve complete data hiding concept, and you cannot have any concrete method or implementation in interface.- You can
extendan abstract class andimplementan interface
For example (JAVA):
abstract class MyClass {
/// Methods are defined with implementation, partial abstraction
void someFunc() {}
void anotherFunc() {}
}
interface MyInterface {
/// Methods are only defined, pure abstraction
void myMethod();
void anotherMethod();
}- It uses
abstractclasses to achieve the goal as of stand aloneabstractandinterfaceboth. - You can define abstract and concrete methods in
abstractclass in dart which is not the case in Java as in above example.
For example (DART):
abstract class DartAbstractClass {
void concreteMethod() {} // concrete method
void abstractMethod(); // abstract method --> Not allowed in Java in abstract class
// Hence, this class is playing role of abstract class and that of
// interface simultaneously in dart
}
// We don't need interface in dart explicitly, bcz this can be achieved
// using abstract classes.
interface class DartInterfaceClass {
// NOTE: This is opposite of Java, bcz in dart this represents
// that if you are using interface then it must provide a concrete
// implementation of each method whenever a class implements it.
void concreteMethod() {} // Not allowed in Java
void anotherConcrete() {} // Not allowed in Java
}❗️ Important note:
// This doesn't exits in dart, it has to be a `class` keyword following it.
interface DartInterface {}Both of these works the same as in other languages i.e.
extendsthe child class doesn't have to provide concrete implementation of each method from parent class.- Generally used for extensions via
abstract class
- Generally used for extensions via
implementsthe child class should provide concrete implementation of each method from parent class.- Generally used for extensions via
interfaceor in case of dart,implementscan be used for anabstract classconsidering that it's playing the role ofinterface
- Generally used for extensions via
In dart terms, an abstract can be extends or implements:
abstract class DartClass {
void abstractMethod();
void conreteMethod() {}
}
/// implements, it means we have to provide concrete implementation of
/// [abstractMethod] and [conreteMethod]
class DartClassImplements implements DartClass {
@override
void abstractMethod() {
print('Concrete method');
}
@override
void abstractMethod() {
print('Another concrete method');
}
}
/// extends, it means we can `override` anyone of the parent's class methods
/// we do not need to `override` all of them.
class DartClassExtends extends DartClass {
@override
void concreteMethod() {
print('Concrete method');
}
}Consider an example below:
abstract interface class DartClass {
void abstractMethod();
void concreteMethod() {}
}Dart allows this because it offers flexibility in how you define and use abstract classes and interfaces.
abstractKeyword: Indicates thatDartClasscannot be instantiated directly. It serves as a blueprint for other classes.interfaceKeyword: Explicitly marksDartClassas an interface, further emphasizing its role as a contract.
void concreteMethod();: A purely abstract method. Implementing classes must provide the implementation.void anotherConcrete() {}: An abstract method with an empty body. While it has a body, it still needs to be overridden in implementing classes because of the interface and abstract keywords.
Complete example:
class DartClassExtends extends DartClass {
/// This is a concrete method implementation of the abstract method
/// declared in the [DartClass] interface.
@override
void abstractMethod() {
print('Abstract method implementation');
}
/// We do not need to override the [concreteMethod] method as it is
}
class DartClassImplements implements DartClass {
/// In this case, we need to implement both the [abstractMethod] and
/// [concreteMethod] methods as they are declared in the [DartClass]
@override
void abstractMethod() {
print('Abstract method implementation');
}
@override
void concreteMethod() {
print('Concrete method implementation');
}
}