Inheritance is an important concept in object-oriented programming that allows a class to inherit properties and methods from another class. In Dart, you can implement inheritance using the extends
keyword.
To create an inheritance relationship between two classes, you need to define a base class (also known as a superclass) and a derived class (also known as a subclass). The derived class inherits all the properties and methods of the base class.
Here's an example code snippet to help you understand how to implement inheritance in Dart:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class Animal { String name; Animal(this.name); void eat() { print('$name is eating.'); } void sleep() { print('$name is sleeping.'); } } class Dog extends Animal { Dog(String name) : super(name); void bark() { print('$name is barking.'); } } void main() { var dog = Dog('Buddy'); print(dog.name); // Output: Buddy dog.eat(); // Output: Buddy is eating. dog.sleep(); // Output: Buddy is sleeping. dog.bark(); // Output: Buddy is barking. } |
In the example, we have defined two classes - Animal
and Dog
. The Animal
class serves as the base class with properties like name
and methods like eat()
and sleep()
. The Dog
class is derived from the Animal
class using the extends
keyword and adds its own method called bark()
. To call the superclass's constructor, we use super(name)
in the derived class's constructor.
In the main()
function, we create an instance of the Dog
class and demonstrate that it has access to all the properties and methods of the Animal
class through inheritance.
How to check if an object is an instance of a particular class in Dart inheritance?
To check if an object is an instance of a particular class in Dart inheritance, you can use the is
keyword followed by the class name.
Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Animal { // ... } class Dog extends Animal { // ... } void main() { var dog = Dog(); // Check if 'dog' is an instance of 'Animal' if (dog is Animal) { print('dog is an instance of Animal class'); } // Check if 'dog' is an instance of 'Dog' if (dog is Dog) { print('dog is an instance of Dog class'); } } |
In the above example, the dog
object is first checked against the Animal
class using the is
keyword. If it is an instance of the Animal
class, the first if
condition will be true and the corresponding message will be printed. Similarly, the dog
object is then checked against the Dog
class, and if it is an instance of the Dog
class, the second if
condition will be true and the message will be printed.
How to create a class that inherits from another class in Dart?
To create a class that inherits from another class in Dart, you can use the extends
keyword followed by the name of the superclass. Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Animal { void sound() { print('Animal makes sound'); } } class Dog extends Animal { @override void sound() { print('Dog barks'); } } void main() { Dog dog = Dog(); dog.sound(); // Output: Dog barks } |
In the above example, the Dog
class inherits from the Animal
class using the extends
keyword. The Dog
class then overrides the sound()
method of the superclass to provide its own implementation.
By using the @override
annotation before the sound()
method in the Dog
class, it explicitly indicates that the method is intended to override the same method in the superclass. This is not mandatory in Dart, but it helps to prevent accidental method name typos or incorrect signatures.
When you create an instance of the Dog
class and invoke the sound()
method, it will call the overridden implementation in the Dog
class, which prints "Dog barks" to the console.
What is inheritance in Dart?
Inheritance in Dart is a way to create new classes based on existing classes. It allows a class to inherit properties and methods from another class, known as the parent class or superclass.
A child class, also known as a subclass, can inherit properties and methods from the parent class and can also add its own additional properties and methods. This promotes code reusability and allows for the creation of more specialized classes.
To establish an inheritance relationship between classes in Dart, the keyword extends
is used. By extending a class, the child class inherits all of the properties and methods of the parent class. The child class can then override inherited methods or add new methods and properties.
Here's an example of inheritance in Dart:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Animal { String name; void eat() { print('$name is eating.'); } } class Dog extends Animal { void bark() { print('$name is barking.'); } } void main() { Dog dog = Dog(); dog.name = 'Buddy'; dog.eat(); dog.bark(); } |
In the above code, the Dog
class extends the Animal
class using the extends
keyword. As a result, the Dog
class inherits the name
property and eat()
method from the Animal
class. Then, a new method bark()
is added to the Dog
class.
The main()
function demonstrates the usage of the classes. The dog
object is created as an instance of the Dog
class, and it can access both the inherited method eat()
and the new method bark()
.
Output:
1 2 |
Buddy is eating. Buddy is barking. |
In this way, inheritance provides a way to build hierarchies of classes, enabling code reuse and creating more specialized classes by deriving from existing ones.