-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path29. polymorphism.dart
167 lines (138 loc) · 5.02 KB
/
29. polymorphism.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/// Polymorphism in Dart
///
/// What is Polymorphism?
///
/// Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common type.
/// The word "polymorphism" comes from the Greek words "poly" (many) and "morph" (form), meaning "many forms."
/// In essence, polymorphism enables you to write code that can work with objects of different classes without needing to know their specific types at compile time.
///
/// Key Concepts of Polymorphism:
///
/// 1.Common Type: Polymorphism relies on a common type (superclass, interface, or abstract class) that defines a set of shared behaviors.
/// 2.Subtype Polymorphism: This is the most common type of polymorphism, achieved through inheritance. Subclasses inherit from a superclass and can override its methods.
/// 3.Method Overriding: Subclasses can provide their own specific implementations of methods defined in the superclass.
/// 4.Dynamic Dispatch: At runtime, the appropriate method implementation is selected based on the actual type of the object.
/// 5.Interface Polymorphism: Achieved through interfaces (abstract classes in Dart). Classes that implement the same interface can be treated as objects of that interface type.
/// 6.Flexibility: Polymorphism allows you to write more flexible and adaptable code that can work with a variety of object types.
///
/// Benefits of Polymorphism:
///
/// 1.Code Reusability: You can write code that works with objects of a common type, regardless of their specific class.
/// 2.Extensibility: You can easily add new classes that conform to the common type without modifying existing code.
/// 3.Maintainability: Polymorphism makes code easier to maintain because changes to one class are less likely to affect other parts of the code.
/// 4.Abstraction: Polymorphism allows you to work at a higher level of abstraction, focusing on what objects can do rather than how they do it.
/// 5.Flexibility: Polymorphism enables you to write more flexible and adaptable code that can work with a variety of object types.
///
/// How Polymorphism is Achieved in Dart:
///
/// 1.Inheritance: Subtype polymorphism is achieved through inheritance using the `extends` keyword.
/// 2.Method Overriding: Subclasses override methods from the superclass using the `@override` annotation.
/// 3.Abstract Classes: Abstract classes define interfaces that can be implemented by multiple classes.
///
/// Example 1: Subtype Polymorphism with Inheritance
///
/// Let's illustrate subtype polymorphism with an `Animal` superclass and `Dog` and `Cat` subclasses.
class Animal {
String name;
Animal(this.name);
void makeSound() {
print('$name is making a sound.');
}
}
class Dog extends Animal {
Dog(String name) : super(name);
@override
void makeSound() {
print('$name says Woof!');
}
}
class Cat extends Animal {
Cat(String name) : super(name);
@override
void makeSound() {
print('$name says Meow!');
}
}
/// Example 2: Polymorphism with a List of Different Objects
///
/// This example demonstrates how to use polymorphism with a list of different objects.
class Vehicle {
String brand;
String model;
Vehicle(this.brand, this.model);
void start() {
print('$brand $model is starting.');
}
}
class Car extends Vehicle {
Car(String brand, String model) : super(brand, model);
@override
void start() {
print('Car $brand $model engine is revving.');
}
}
class Motorcycle extends Vehicle {
Motorcycle(String brand, String model) : super(brand, model);
@override
void start() {
print('Motorcycle $brand $model engine is roaring.');
}
}
/// Example 3: Polymorphism with Abstract Classes
///
/// This example demonstrates polymorphism using an abstract class.
abstract class Shape {
double getArea();
}
class Circle implements Shape {
double radius;
Circle(this.radius);
@override
double getArea() {
return 3.14159 * radius * radius;
}
}
class Square implements Shape {
double side;
Square(this.side);
@override
double getArea() {
return side * side;
}
}
void main() {
// Example 1: Subtype Polymorphism
print('--- Example 1 ---');
Animal animal1 = Dog('Buddy');
Animal animal2 = Cat('Whiskers');
animal1.makeSound(); // Output: Buddy says Woof!
animal2.makeSound(); // Output: Whiskers says Meow!
// Example 2: Polymorphism with a List
print('\n--- Example 2 ---');
List<Vehicle> vehicles = [
Car('Toyota', 'Camry'),
Motorcycle('Harley-Davidson', 'Sportster'),
Car('Honda', 'Civic'),
];
for (var vehicle in vehicles) {
vehicle.start();
}
// Output:
// Car Toyota Camry engine is revving.
// Motorcycle Harley-Davidson Sportster engine is roaring.
// Car Honda Civic engine is revving.
// Example 3: Polymorphism with Abstract Classes
print('\n--- Example 3 ---');
List<Shape> shapes = [
Circle(5.0),
Square(4.0),
Circle(2.5),
];
for (var shape in shapes) {
print('Area: ${shape.getArea()}');
}
// Output:
// Area: 78.53975
// Area: 16.0
// Area: 19.6349375
}