Inheritance in Dart
Inheritance is a feature of object-oriented programming that allows a class to inherit properties and behaviors from another class. In Dart, a subclass is created by extending a superclass.
class Animal {
String name;
Animal(this.name);
void makeSound() {
print('$name makes a sound');
}
}
class Dog extends Animal {
Dog(String name) : super(name);
void bark() {
print('$name barks');
}
}
void main() {
var dog = Dog('Fido');
dog.makeSound();
dog.bark();
}
No comments:
Post a Comment