Method overriding occurs in dart when a child class tries to override the parent class’s method. When a child class extends a parent class, it gets full access to the methods of the parent class and thus it overrides the methods of the parent class. It is achieved by re-defining the same method present in the parent class.
This method is helpful when you have to perform different functions for a different child class, so we can simply re-define the content by overriding it.
- A method can be overridden only in the child class, not in the parent class itself.
- Both the methods defined in the child and the parent class should be the exact copy, from name to argument list except the content present inside the method i.e. it can and can’t be the same.
- A method declared final or static inside the parent class can’t be overridden by the child class.
- Constructors of the parent class can’t be inherited, so they can’t be overridden by the child class.
Example 1: Simple case of method overriding.
This is class SuperGeek. This is class SubGeek child of SuperGeek.
Example 2: When there is more than one child class.
This is class SuperGeek. This is class SubGeek1 child of SuperGeek. This is class SubGeek2 child of SuperGeek.
- How to Combine Lists in Dart?
- Basics of Numbers in Dart
- Dart - Super and This keyword
- Dart - Standard Input Output
- Generators in Dart
- Dart - Classes And Objects
- Operators in Dart
- Dart - Loops
- Switch Case in Dart
- Dart - Loop Control Statements (Break and Continue)
- Dart Programming - Functions
- Instance and class methods in Dart
- Abstract Classes in Dart
- Labels in Dart
- Dart Programming - List
- Different Types of Functions in Dart
- Dart - Splitting of String
- Concept of Isolates in Dart
- Concept of Callable Classes in Dart
- Getter and Setter Methods in Dart
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.