Reputation: 35
I was told that to make use of a class we create it's object but in flutter we extend our class with stateless or statefull widgets without creating objects of it, now you may say we can't create objects out of abstract class but what I'm asking is how is it working and why did they use abstract class at first place.
Upvotes: 1
Views: 851
Reputation: 63614
For the abstract
Dart has no interface keyword. Instead, all classes implicitly define an interface. Therefore, you can implement any class.
So we have two general way to use this abstract class. One is by implements
where we need to override all methods and another one extend
the class.
An example code
abstract class A {
final int data = 1;
}
class B implements A {
@override
int get data => 2;
}
class C extends A {}
void main(List<String> args) {
final itemB = B();
print(itemB.data); // print 2
final itemC = C();
print(itemC.data); // print 1
}
As you can see, you are not force to override and initialize value on extends because its extends duty to get parent behavior.
While flutter is a framework, and framework means
a software framework is an abstraction in which software, providing generic functionality, can be selectively changed by additional user-written code, thus providing application-specific software.... more on wikipedia
So Why we extends StatelessWidget
and StatefulWidget
, Because we are using a flutter framework, and we don't want to create things from scratch, we just use and override provided functionality based on our need.
This is a taught concept for me to describe, but tried my best to simplify it. Feel free to update the answer if you find any mistake.
Upvotes: 1