-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path33. interface.dart
196 lines (167 loc) · 6.16 KB
/
33. interface.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
/// Interfaces in Dart
///
/// Introduction:
///
/// In Dart, interfaces are implemented using *abstract classes*.
/// Dart doesn't have a specific `interface` keyword like some other languages (e.g., Java, C#).
/// Instead, you use abstract classes to define a contract that other classes can implement.
/// An interface defines a set of methods that a class must implement if it claims to adhere to that interface.
///
/// Key Concepts:
///
/// 1.Abstract Classes as Interfaces: Abstract classes in Dart can act as interfaces by defining a set of abstract methods.
/// 2.Contract: An interface defines a contract that classes must follow. If a class implements an interface, it must provide concrete implementations for all the abstract methods defined in that interface.
/// 3.Implementation: Classes implement interfaces using the `implements` keyword.
/// 4.Multiple Interfaces: A class can implement multiple interfaces, allowing it to conform to multiple contracts.
/// 5.No Implementation in Interface: Interfaces (abstract classes used as interfaces) typically do not contain any concrete method implementations. They primarily define the method signatures.
/// 6.Polymorphism: Interfaces enable polymorphism, allowing you to treat objects of different classes as objects of the interface type.
/// 7.Abstraction: Interfaces provide a high level of abstraction, focusing on *what* an object can do rather than *how* it does it.
///
/// Benefits of Using Interfaces:
///
/// 1.Defining Contracts: Interfaces define clear contracts that classes must adhere to, ensuring consistency and predictability.
/// 2.Loose Coupling: Interfaces promote loose coupling between classes, making it easier to change the implementation of one class without affecting others.
/// 3.Code Reusability: Interfaces allow you to write code that works with objects of any class that implements the interface, regardless of their specific type.
/// 4.Extensibility: You can easily add new classes that conform to an existing interface without modifying the code that uses the interface.
/// 5.Maintainability: Interfaces make code more maintainable by clearly defining the relationships between classes.
/// 6.Testability: Interfaces make it easier to write unit tests by allowing you to mock or stub out dependencies.
/// 7.Polymorphism: Interfaces enable polymorphism, allowing you to treat objects of different classes as objects of the interface type.
///
/// How Interfaces are Defined and Implemented:
///
/// 1.Defining an Interface:
/// - Create an abstract class with abstract methods.
/// - Abstract methods are defined without a method body (no `{}`), and they end with a semicolon (`;`).
///
/// ```dart
/// abstract class MyInterface {
/// void myMethod1(); // Abstract method
/// int myMethod2(String param); // Abstract method
/// }
/// ```
///
/// 2.Implementing an Interface:
/// - Use the `implements` keyword followed by the interface name.
/// - Provide concrete implementations for all the abstract methods defined in the interface.
///
/// ```dart
/// class MyClass implements MyInterface {
/// @override
/// void myMethod1() {
/// // Implementation for myMethod1
/// }
///
/// @override
/// int myMethod2(String param) {
/// // Implementation for myMethod2
/// return 0;
/// }
/// }
/// ```
///
/// Example 1: Basic Interface Implementation
///
/// This example demonstrates a simple interface `Shape` and two classes `Circle` and `Square` that implement it.
abstract class Shape {
double getArea(); // Abstract method
}
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;
}
}
/// Example 2: Multiple Interface Implementation
///
/// This example demonstrates a class implementing multiple interfaces.
abstract class Flyable {
void fly();
}
abstract class Swimmable {
void swim();
}
class Duck implements Flyable, Swimmable {
@override
void fly() {
print('Duck is flying.');
}
@override
void swim() {
print('Duck is swimming.');
}
}
/// Example 3: Interface in `Vehicle` Class
///
/// This example demonstrates how to use an interface in a class.
abstract class Startable {
void start();
}
abstract class Stoppable {
void stop();
}
class Car implements Startable, Stoppable {
String brand;
String model;
Car(this.brand, this.model);
@override
void start() {
print('Car $brand $model engine is revving.');
}
@override
void stop() {
print('Car $brand $model is stopping.');
}
}
class Motorcycle implements Startable, Stoppable {
String brand;
String model;
Motorcycle(this.brand, this.model);
@override
void start() {
print('Motorcycle $brand $model engine is roaring.');
}
@override
void stop() {
print('Motorcycle $brand $model is stopping.');
}
}
void main() {
// Example 1: Basic Interface Implementation
print('--- Example 1 ---');
Circle circle1 = Circle(5.0);
print('Circle area: ${circle1.getArea()}'); // Output: Circle area: 78.53975
Square square1 = Square(4.0);
print('Square area: ${square1.getArea()}'); // Output: Square area: 16.0
// Example 2: Multiple Interface Implementation
print('\n--- Example 2 ---');
Duck duck1 = Duck();
duck1.fly(); // Output: Duck is flying.
duck1.swim(); // Output: Duck is swimming.
// Example 3: Interface in `Vehicle` Class
print('\n--- Example 3 ---');
Car car1 = Car('Toyota', 'Camry');
car1.start(); // Output: Car Toyota Camry engine is revving.
car1.stop(); // Output: Car Toyota Camry is stopping.
Motorcycle motorcycle1 = Motorcycle('Harley-Davidson', 'Sportster');
motorcycle1.start(); // Output: Motorcycle Harley-Davidson Sportster engine is roaring.
motorcycle1.stop(); // Output: Motorcycle Harley-Davidson Sportster is stopping.
// Polymorphism Example
print('\n--- Polymorphism Example ---');
List<Startable> startables = [car1, motorcycle1];
for (var startable in startables) {
startable.start();
}
// Output:
// Car Toyota Camry engine is revving.
// Motorcycle Harley-Davidson Sportster engine is roaring.
}