-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path27. inheritance_constructors.dart
140 lines (116 loc) · 4.49 KB
/
27. inheritance_constructors.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
/// Inheritance of Constructors in Dart
///
/// In Dart, when you use inheritance (the `extends` keyword), subclasses inherit the properties and methods of their superclass.
/// However, constructors are *not* inherited in the same way.
/// Each class has its own set of constructors.
/// When you create a subclass, you need to define its constructors, and you can use the `super` keyword to call the superclass's constructor to initialize the inherited fields.
///
/// Key Concepts:
///
/// 1.Constructors are Not Inherited: Subclasses do not automatically inherit the constructors of their superclass.
/// 2.The `super` Keyword: The `super` keyword is used within a subclass's constructor to call the superclass's constructor.
/// 3.Initializing Superclass Fields: The `super` call is essential for initializing the fields that the subclass inherits from the superclass.
/// 4.Constructor Chaining: You can chain constructors within a class using the `this` keyword, but this is not directly related to inheritance.
/// 5.Default Constructor: If a superclass has no explicitly defined constructor, it has an implicit default constructor (with no parameters).
/// 6.Named Constructors: Subclasses can call named constructors of the superclass using `super.namedConstructorName()`.
/// 7.Initializer Lists: Initializer lists (using `:`) can be used in conjunction with `super` to initialize fields before the constructor body runs.
///
/// Rules for Calling Superclass Constructors:
///
/// 1.The `super` call must be the first thing in the subclass's constructor's initializer list (if present) or body.
/// 2.If the superclass has a default constructor (no parameters), you don't need to explicitly call `super()` if you don't need to pass any arguments. Dart will call it implicitly.
/// 3.If the superclass has only parameterized constructors, you *must* call one of them using `super(...)`.
/// 4.If the superclass has named constructors, you can call them using `super.namedConstructorName(...)`.
///
/// Example 1: Basic Inheritance with Constructors
///
/// This example demonstrates a simple inheritance scenario with a superclass `Animal` and a subclass `Dog`.
// ignore_for_file: unused_local_variable
class Animal {
String name;
int age;
Animal(this.name, this.age) {
print('Animal constructor called.');
}
Animal.unknown()
: name = 'Unknown',
age = 0 {
print('Animal.unknown constructor called.');
}
}
class Dog extends Animal {
String breed;
Dog(String name, int age, this.breed) : super(name, age) {
print('Dog constructor called.');
}
Dog.unknown(this.breed) : super.unknown() {
print('Dog.unknown constructor called.');
}
}
/// Example 2: Inheritance with Named Constructors
///
/// This example demonstrates how to call a named constructor of the superclass.
class Vehicle {
String brand;
String model;
Vehicle(this.brand, this.model) {
print('Vehicle constructor called.');
}
Vehicle.unknown()
: brand = 'Unknown',
model = 'Unknown' {
print('Vehicle.unknown constructor called.');
}
}
class Car extends Vehicle {
int numberOfDoors;
Car(String brand, String model, this.numberOfDoors) : super(brand, model) {
print('Car constructor called.');
}
Car.unknown(this.numberOfDoors) : super.unknown() {
print('Car.unknown constructor called.');
}
}
/// Example 3: Inheritance with Initializer Lists
///
/// This example shows how to use initializer lists with `super`.
class Shape {
String color;
Shape(this.color) {
print('Shape constructor called.');
}
}
class Circle extends Shape {
double radius;
Circle(String color, this.radius) : super(color) {
print('Circle constructor called.');
}
}
void main() {
// Example 1: Basic Inheritance
print('--- Example 1 ---');
Dog dog1 = Dog('Buddy', 3, 'Golden Retriever');
// Output:
// Animal constructor called.
// Dog constructor called.
Dog dog2 = Dog.unknown('Poodle');
// Output:
// Animal.unknown constructor called.
// Dog.unknown constructor called.
// Example 2: Inheritance with Named Constructors
print('\n--- Example 2 ---');
Car car1 = Car('Toyota', 'Camry', 4);
// Output:
// Vehicle constructor called.
// Car constructor called.
Car car2 = Car.unknown(2);
// Output:
// Vehicle.unknown constructor called.
// Car.unknown constructor called.
// Example 3: Inheritance with Initializer Lists
print('\n--- Example 3 ---');
Circle circle1 = Circle('Red', 5.0);
// Output:
// Shape constructor called.
// Circle constructor called.
}