Consider a hierarchy of classes like so:
- There is a single base class,
BaseClass - There are many derived classes from
BaseClass, some of which implement different interfaces
So for example, there's a class DerivedClass that extends BaseClass and implements SomeInterface.
Since I'm dealing with many different classes that all inherit from BaseClass, I figured that I would store their objects in a container like List<BaseClass>. But then I can't seem to figure out a way to detect an interface in those objects.
class SomeInterface {
String field;
}
abstract class BaseClass {
int count;
BaseClass(this.count);
}
class DerivedClass extends BaseClass implements SomeInterface {
String field;
DerivedClass(int count, this.field) : super(count);
}
void printField(SomeInterface obj) {
print(obj.field);
}
void main() {
BaseClass item = DerivedClass(4, 'test');
if (item is SomeInterface) { // Attempt one
print(item.field);
}
printField(item); // Attempt two
}
Earlier I had SomeInterface in the inheritance chain like an actual superclass, but I'd like to stay away from that as interfaces are easier to handle in my case. What would you recommend?