I have class A:
class A{
String title;
String content;
IconData iconData;
Function onTab;
A({this.title, this.content, this.iconData, this.onTab});
}
How can i create class B that extends class A with additional variable like following:
class B extends A{
bool read;
B({this.read});
}
Tried with this but not working
let o = new B(
title: "New notification",
iconData: Icons.notifications,
content: "Lorem ipsum doro si maet 100",
read: false,
onTab: (context) => {
});
You can inherit from or extend a class using the extends keyword. This allows you share properties and methods between classes that are similar, but not exactly the same. Also, it allows different subtypes to share a common runtime type so that static analysis doesn't fail.
In Dart, the extends keyword is typically used to alter the behavior of a class using Inheritance. The capability of a class to derive properties and characteristics from another class is called Inheritance. It is ability of a program to create new class from an existing class.
Dart has a Function type. This can be extended on and you can pass type parameters if you want. Here is an example from the changelog: extension CurryFunction<R, S, T> on R Function(S, T) { ... }
You have to define the constructor on the child class.
class B extends A {
bool read;
B({title, content, iconData, onTab, this.read}) : super(title: title, content: content, iconData: iconData, onTab: onTab);
}
You can inherit from or extend a class using the extends keyword. This allows you share properties and methods between classes that are similar, but not exactly the same. Also, it allows different subtypes to share a common runtime type so that static analysis doesn't fail. (More on this below); The classic example is using different types of animals.
class Animal {
Animal(this.name, this.age);
int age;
String name;
void talk() {
print('grrrr');
}
}
class Cat extends Animal {
// use the 'super' keyword to interact with
// the super class of Cat
Cat(String name, int age) : super(name, age);
void talk() {
print('meow');
}
}
class Dog extends Animal {
// use the 'super' keyword to interact with
// the super class of Cat
Dog(String name, int age) : super(name, age);
void talk() {
print('bark');
}
}
void main() {
var cat = Cat("Phoebe",1);
var dog = Dog("Cowboy", 2);
dog.talk();
cat.talk();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With