Inheritance in Dart

 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

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...