-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#30-Inheritence_constructor.dart
49 lines (46 loc) · 1.8 KB
/
#30-Inheritence_constructor.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
void main(){
//Object oriented Programming in Dart.
//Inheritance with constructor
var dog1 = Dog("Labrador","red");
print("");
dog1.bark();
print("");
var dog2 = Dog.namedConst();
}
//A constructor in sub class calls the super class no argument constructor.
//so in the upper scenario we don't have to use "super" keyword.
class Animal{
var color;
Animal(){ //it is mandatory that your superclass should not have arguments. or it will show error
print("Hey i am a Animal constructor \"this will always exexute\".");//declared default constructor for Dog
}
Animal.myAnimalNamedCons(){
print("animal named constuctor execute in child class");
}
}
//if you have argument in the main class you have to code it on the child class with super keyword. like this - ->
/*class Dog extends Animal{
Dog(String breed,String color):super(color){
print("Hey i am a $breed constructor $color");//declared default constructor for Dog
}
void bark(){
print("Dog barks!");
}
}*/
class Dog extends Animal{
//we can also call named constructor here at default constructor.
Dog(String breed,String color){//inheritance with default constructor
print("Hey i am a $breed constructor $color");//declared default constructor for Dog
}
//here we call "Animal" Named constructor
Dog.namedConst():super.myAnimalNamedCons(){//inheritance with Named constructor
print("Hey i am a named constructor ");//declared name constructor for Dog
}
void bark(){
print("Dog barks!");
}
}
//if we Don't call the parent class constructor it will be executed Normally like we are calling super class.
//if we pass a parameter this sequence will not be distracted.
//parent class constuctor is called before the child class.
//if we pass a parameter this sequence will not be distracted.