-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26. inheritance.dart
166 lines (137 loc) · 5.39 KB
/
26. inheritance.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
/// Inheritance in Dart
///
/// What is Inheritance?
///
/// Inheritance is a fundamental concept in object-oriented programming (OOP) that allows you to create a new class (subclass/child class) based on an existing class (superclass/parent class).
/// The subclass inherits the properties (fields) and behaviors (methods) of the superclass, and it can also add its own unique properties and behaviors.
///
/// Key Concepts of Inheritance:
///
/// 1.Superclass (Parent Class): The existing class from which another class inherits. It defines common properties and behaviors.
/// 2.Subclass (Child Class): The new class that inherits from the superclass. It extends the superclass by adding its own specific properties and behaviors.
/// 3.Inheritance Relationship: The "is-a" relationship between the subclass and the superclass. For example, a "Dog" is an "Animal."
/// 4.Code Reusability: Inheritance promotes code reuse by allowing you to define common properties and behaviors in a superclass and reuse them in multiple subclasses.
/// 5.Extensibility: Subclasses can extend the functionality of the superclass by adding new properties and methods.
/// 6.Overriding: Subclasses can override (redefine) methods inherited from the superclass to provide their own specific implementation.
/// 7.Polymorphism: Inheritance enables polymorphism, which means that objects of different classes can be treated as objects of a common superclass.
///
/// Benefits of Inheritance:
///
/// 1.Code Reusability: Reduces code duplication by allowing you to reuse common code in multiple classes.
/// 2.Organization: Helps organize code into a hierarchy of related classes, making it easier to understand and maintain.
/// 3.Extensibility: Makes it easy to extend the functionality of existing classes without modifying their original code.
/// 4.Polymorphism: Enables polymorphism, which allows you to write more flexible and adaptable code.
/// 5.Abstraction: Hides implementation details of the superclass from the subclass.
///
/// How Inheritance is Achieved in Dart:
///
/// 1.The `extends` Keyword: In Dart, you use the `extends` keyword to indicate that a class inherits from another class.
///
/// Syntax:
///
/// ```dart
/// class Subclass extends Superclass {
/// // Subclass properties and methods
/// }
/// ```
///
/// 2.The `super` Keyword: The `super` keyword is used within a subclass to refer to the superclass.
/// - It can be used to call the superclass's constructor or methods.
///
/// Example 1: The `Animal`, `Dog`, and `Cat` Classes
///
/// Let's illustrate inheritance with an `Animal` superclass and `Dog` and `Cat` subclasses.
class Animal {
String name;
int age;
Animal(this.name, this.age);
void eat() {
print('$name is eating.');
}
void sleep() {
print('$name is sleeping.');
}
void makeSound() {
print('$name is making a sound.');
}
}
class Dog extends Animal {
String breed;
Dog(String name, int age, this.breed) : super(name, age);
@override
void makeSound() {
print('$name says Woof!');
}
void fetch() {
print('$name is fetching.');
}
}
class Cat extends Animal {
String color;
Cat(String name, int age, this.color) : super(name, age);
@override
void makeSound() {
print('$name says Meow!');
}
void climb() {
print('$name is climbing.');
}
}
/// Example 2: The `Vehicle`, `Car`, and `Motorcycle` Classes
///
/// Let's illustrate inheritance with an `Vehicle` superclass and `Car` and `Motorcycle` subclasses.
class Vehicle {
String brand;
String model;
int year;
Vehicle(this.brand, this.model, this.year);
void start() {
print('$brand $model is starting.');
}
void stop() {
print('$brand $model is stopping.');
}
}
class Car extends Vehicle {
int numberOfDoors;
Car(String brand, String model, int year, this.numberOfDoors)
: super(brand, model, year);
void drive() {
print('Driving the $brand $model with $numberOfDoors doors.');
}
}
class Motorcycle extends Vehicle {
bool hasSidecar;
Motorcycle(String brand, String model, int year, this.hasSidecar)
: super(brand, model, year);
void wheelie() {
print('Doing a wheelie with the $brand $model!');
}
}
void main() {
// Example 1: Animal, Dog, and Cat
Dog dog1 = Dog('Buddy', 3, 'Golden Retriever');
dog1.eat(); // Output: Buddy is eating.
dog1.sleep(); // Output: Buddy is sleeping.
dog1.makeSound(); // Output: Buddy says Woof!
dog1.fetch(); // Output: Buddy is fetching.
Cat cat1 = Cat('Whiskers', 2, 'Gray');
cat1.eat(); // Output: Whiskers is eating.
cat1.sleep(); // Output: Whiskers is sleeping.
cat1.makeSound(); // Output: Whiskers says Meow!
cat1.climb(); // Output: Whiskers is climbing.
// Example 2: Vehicle, Car, and Motorcycle
Car car1 = Car('Toyota', 'Camry', 2022, 4);
car1.start(); // Output: Toyota Camry is starting.
car1.drive(); // Output: Driving the Toyota Camry with 4 doors.
car1.stop(); // Output: Toyota Camry is stopping.
Motorcycle motorcycle1 = Motorcycle('Harley-Davidson', 'Sportster', 2020, false);
motorcycle1.start(); // Output: Harley-Davidson Sportster is starting.
motorcycle1.wheelie(); // Output: Doing a wheelie with the Harley-Davidson Sportster!
motorcycle1.stop(); // Output: Harley-Davidson Sportster is stopping.
// Polymorphism Example
List<Animal> animals = [dog1, cat1];
for (var animal in animals) {
animal.makeSound(); // Output: Buddy says Woof!, Whiskers says Meow!
}
}