-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path32. abstract.dart
231 lines (190 loc) · 7.07 KB
/
32. abstract.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/// Abstract Classes in Dart
///
/// What are Abstract Classes?
///
/// In Dart, an *abstract class* is a class that cannot be instantiated directly.
/// It serves as a blueprint for other classes (subclasses) to inherit from.
/// Abstract classes are used to define a common interface or set of behaviors that subclasses must implement.
/// They can contain both concrete (implemented) methods and abstract (unimplemented) methods.
///
/// Key Characteristics of Abstract Classes:
///
/// 1.Cannot be Instantiated: You cannot create an object directly from an abstract class using `new`.
/// 2.Blueprint for Subclasses: Abstract classes define a common structure and set of behaviors that subclasses must adhere to.
/// 3.Abstract Methods: Abstract classes can contain *abstract methods*, which are method declarations without an implementation. Subclasses *must* provide an implementation for these methods.
/// 4.Concrete Methods: Abstract classes can also contain *concrete methods*, which are methods with a full implementation. Subclasses can use these methods as-is or override them.
/// 5.Interface-Like: Abstract classes define an interface that subclasses must conform to, similar to how interfaces work in other languages.
/// 6.Partial Implementation: Abstract classes can provide a partial implementation of a class, leaving some methods to be implemented by subclasses.
/// 7.`abstract` Keyword: Abstract classes are defined using the `abstract` keyword before the `class` keyword.
/// 8.Inheritance: Abstract classes are designed to be extended using the `extends` keyword.
///
/// Benefits of Using Abstract Classes:
///
/// 1.Defining Interfaces: Abstract classes are a way to define interfaces in Dart, ensuring that subclasses implement a specific set of methods.
/// 2.Code Reusability: Concrete methods in abstract classes can be reused by subclasses, reducing code duplication.
/// 3.Enforcing Structure: Abstract classes enforce a common structure and set of behaviors across related classes.
/// 4.Abstraction: They allow you to work at a higher level of abstraction, focusing on what objects can do rather than how they do it.
/// 5.Polymorphism: Abstract classes enable polymorphism, allowing you to treat objects of different subclasses as objects of the abstract class type.
/// 6.Flexibility: Abstract classes provide a flexible way to define a common interface while allowing subclasses to implement the details in their own way.
///
/// How Abstract Classes are Defined:
///
/// Abstract classes are defined using the `abstract` keyword before the `class` keyword.
/// Abstract methods are defined without a method body (no `{}`), and they end with a semicolon (`;`).
///
/// Syntax:
///
/// ```dart
/// abstract class AbstractClassName {
/// // Abstract method (no implementation)
/// returnType abstractMethodName(parameters);
///
/// // Concrete method (with implementation)
/// returnType concreteMethodName(parameters) {
/// // Method body
/// }
/// }
/// ```
///
/// Example 1: Basic Abstract Class
///
/// This example demonstrates a simple abstract class `Shape` with an abstract method `getArea()`.
abstract class Shape {
// Abstract method (no implementation)
double getArea();
// Concrete method (with implementation)
void describe() {
print('This is a shape.');
}
}
class Circle extends Shape {
double radius;
Circle(this.radius);
@override
double getArea() {
return 3.14159 * radius * radius;
}
}
class Square extends Shape {
double side;
Square(this.side);
@override
double getArea() {
return side * side;
}
}
/// Example 2: Abstract Class with Multiple Abstract Methods
///
/// This example demonstrates an abstract class with multiple abstract methods.
abstract class Animal {
String name;
Animal(this.name);
void eat(); // Abstract method
void sleep(); // Abstract method
void makeSound(); // Abstract method
}
class Dog extends Animal {
String breed;
Dog(String name, this.breed) : super(name);
@override
void eat() {
print('$name is eating.');
}
@override
void sleep() {
print('$name is sleeping.');
}
@override
void makeSound() {
print('$name says Woof!');
}
}
class Cat extends Animal {
String color;
Cat(String name, this.color) : super(name);
@override
void eat() {
print('$name is eating.');
}
@override
void sleep() {
print('$name is sleeping.');
}
@override
void makeSound() {
print('$name says Meow!');
}
}
/// Example 3: Abstract Class in `Vehicle` Class
///
/// This example demonstrates how to use an abstract class in a class.
abstract class Vehicle {
String brand;
String model;
Vehicle(this.brand, this.model);
void start(); // Abstract method
void stop() {
print('$brand $model is stopping.');
}
}
class Car extends Vehicle {
int numberOfDoors;
Car(String brand, String model, this.numberOfDoors) : super(brand, model);
@override
void start() {
print('Car $brand $model engine is revving.');
}
void drive() {
print('Driving the $brand $model with $numberOfDoors doors.');
}
}
class Motorcycle extends Vehicle {
bool hasSidecar;
Motorcycle(String brand, String model, this.hasSidecar) : super(brand, model);
@override
void start() {
print('Motorcycle $brand $model engine is roaring.');
}
void wheelie() {
print('Doing a wheelie with the $brand $model!');
}
}
void main() {
// Example 1: Basic Abstract Class
print('--- Example 1 ---');
Circle circle1 = Circle(5.0);
print('Circle area: ${circle1.getArea()}'); // Output: Circle area: 78.53975
circle1.describe(); // Output: This is a shape.
Square square1 = Square(4.0);
print('Square area: ${square1.getArea()}'); // Output: Square area: 16.0
square1.describe(); // Output: This is a shape.
// Example 2: Abstract Class with Multiple Abstract Methods
print('\n--- Example 2 ---');
Dog dog1 = Dog('Buddy', 'Golden Retriever');
dog1.eat(); // Output: Buddy is eating.
dog1.sleep(); // Output: Buddy is sleeping.
dog1.makeSound(); // Output: Buddy says Woof!
Cat cat1 = Cat('Whiskers', 'Gray');
cat1.eat(); // Output: Whiskers is eating.
cat1.sleep(); // Output: Whiskers is sleeping.
cat1.makeSound(); // Output: Whiskers says Meow!
// Example 3: Abstract Class in `Vehicle` Class
print('\n--- Example 3 ---');
Car car1 = Car('Toyota', 'Camry', 4);
car1.start(); // Output: Car Toyota Camry engine is revving.
car1.drive(); // Output: Driving the Toyota Camry with 4 doors.
car1.stop(); // Output: Toyota Camry is stopping.
Motorcycle motorcycle1 = Motorcycle('Harley-Davidson', 'Sportster', false);
motorcycle1.start(); // Output: Motorcycle Harley-Davidson Sportster engine is roaring.
motorcycle1.wheelie(); // Output: Doing a wheelie with the Harley-Davidson Sportster!
motorcycle1.stop(); // Output: Harley-Davidson Sportster is stopping.
// Polymorphism Example
print('\n--- Polymorphism Example ---');
List<Vehicle> vehicles = [car1, motorcycle1];
for (var vehicle in vehicles) {
vehicle.start();
}
// Output:
// Car Toyota Camry engine is revving.
// Motorcycle Harley-Davidson Sportster engine is roaring.
}