Method Overloading and Method Overriding in Dart
Dart is a versatile, object-oriented programming language that supports method overriding but does not have direct support for method overloading. This blog explains these concepts in detail and provides practical examples to demonstrate their use.
Method Overriding
Method overriding allows a subclass to provide a specific implementation of a method defined in its parent class. This is an essential feature of polymorphism in Dart.
Key Features of Method Overriding:
- The overridden method must have the same name, return type, and parameters as the parent method.
- Use the
@override
annotation for clarity and to avoid errors.
Example:
class Animal {
void sound() {
print("Animal makes a sound");
}
}
class Dog extends Animal {
@override
void sound() {
print("Dog barks");
}
}
void main() {
Animal animal = Animal();
animal.sound(); // Output: Animal makes a sound
Animal dog = Dog();
dog.sound(); // Output: Dog barks
}
Method Overloading
Dart does not natively support method overloading, where multiple methods have the same name but different parameter lists. Instead, you can achieve similar behavior using optional parameters or different method names.
Using Optional Parameters
Optional parameters in Dart allow you to define methods that can accept varying numbers of arguments.
Positional Optional Parameters:
class Calculator {
void add(int a, [int? b]) {
if (b != null) {
print("Sum: \${a + b}");
} else {
print("Single value: \$a");
}
}
}
void main() {
Calculator calc = Calculator();
calc.add(5); // Output: Single value: 5
calc.add(5, 10); // Output: Sum: 15
}
Named Optional Parameters:
class Calculator {
void add({required int a, int b = 0}) {
print("Sum: \${a + b}");
}
}
void main() {
Calculator calc = Calculator();
calc.add(a: 5); // Output: Sum: 5
calc.add(a: 5, b: 10); // Output: Sum: 15
}
Using Different Method Names
Instead of overloading, define methods with unique names for specific tasks.
class Printer {
void printString(String text) {
print("String: \$text");
}
void printNumber(int number) {
print("Number: \$number");
}
}
void main() {
Printer printer = Printer();
printer.printString("Hello");
printer.printNumber(123);
}
Combining Overriding and Optional Parameters
Optional parameters can also be used in overridden methods to extend or modify their behavior.
Example:
class Shape {
void draw({String color = "black"}) {
print("Drawing a shape with color: \$color");
}
}
class Circle extends Shape {
@override
void draw({String color = "red"}) {
print("Drawing a circle with color: \$color");
}
}
void main() {
Shape shape = Shape();
shape.draw(); // Output: Drawing a shape with color: black
Shape circle = Circle();
circle.draw(); // Output: Drawing a circle with color: red
}
Conclusion
While Dart does not directly support method overloading, its flexible optional parameters and method overriding features provide powerful alternatives. Understanding these concepts enables developers to write cleaner, more efficient code.
Want to learn more? Check out the Dart Documentation.
Comments
Post a Comment