File tree 3 files changed +132
-0
lines changed
3 files changed +132
-0
lines changed Original file line number Diff line number Diff line change
1
+ // class practice and extends keywoard
2
+
3
+ class Animal {
4
+ constructor ( name , age ) {
5
+ this . name = name ;
6
+ this . age = age ;
7
+ }
8
+ eat ( ) {
9
+ return `${ this . name } is eating` ;
10
+ }
11
+
12
+ isSuperCute ( ) {
13
+ return this . age <= 1 ;
14
+ }
15
+
16
+ isCute ( ) {
17
+ return true ;
18
+ }
19
+ }
20
+ // const animal1 = new Animal("tom", 2);
21
+ // console.log(animal1);
22
+ // console.log(animal1.eat());
23
+ // console.log(animal1.isSuperCute());
24
+
25
+
26
+ // class and extend
27
+
28
+
29
+
30
+ // Animal is parent class
31
+ // Dog is sub class
32
+
33
+ class Dog extends Animal {
34
+
35
+ }
36
+ const tommy = new Dog ( "tommy" , 3 ) ;
37
+ console . log ( tommy ) ;
38
+ console . log ( tommy . isCute ( ) ) ;
39
+
40
+ // with the help of "extend" keyword we need not to
41
+ // write this much of code
42
+ // class Dog{
43
+ // constructor(name, age){
44
+ // this.name = name;
45
+ // this.age = age;
46
+ // }
47
+ // eat(){
48
+ // return `${this.name} is eating`;
49
+ // }
50
+
51
+ // isSuperCute(){
52
+ // return this.age <= 1;
53
+ // }
54
+
55
+ // isCute(){
56
+ // return true;
57
+ // }
58
+ // }
Original file line number Diff line number Diff line change
1
+ // super keywoard
2
+
3
+ // super --> parent class
4
+
5
+ class Animal {
6
+ constructor ( name , age ) {
7
+ this . name = name ;
8
+ this . age = age ;
9
+ }
10
+ eat ( ) {
11
+ return `${ this . name } is eating` ;
12
+ }
13
+
14
+ isSuperCute ( ) {
15
+ return this . age <= 1 ;
16
+ }
17
+
18
+ isCute ( ) {
19
+ return true ;
20
+ }
21
+ }
22
+ class Dog extends Animal {
23
+ constructor ( name , age , speed ) {
24
+ super ( name , age ) ;
25
+ this . speed = speed ;
26
+ }
27
+ run ( ) {
28
+ return `${ this . name } is running at ${ this . speed } kmph` ;
29
+ }
30
+ }
31
+ // object / instance
32
+ const tommy = new Dog ( "tommy" , 3 , 45 ) ;
33
+ console . log ( tommy ) ;
34
+ console . log ( tommy . run ( ) ) ;
35
+ console . log ( tommy . isCute ( ) ) ;
36
+
37
+
Original file line number Diff line number Diff line change
1
+
2
+ // same method in base class
3
+
4
+ class Animal {
5
+ constructor ( name , age ) {
6
+ this . name = name ;
7
+ this . age = age ;
8
+ }
9
+ eat ( ) {
10
+ return `${ this . name } is eating` ;
11
+ }
12
+
13
+ isSuperCute ( ) {
14
+ return this . age <= 1 ;
15
+ }
16
+
17
+ isCute ( ) {
18
+ return true ;
19
+ }
20
+ }
21
+ class Dog extends Animal {
22
+ constructor ( name , age , speed ) {
23
+ super ( name , age ) ;
24
+ this . speed = speed ;
25
+ }
26
+ eat ( ) {
27
+ return `Modified Eat: ${ this . name } is eating `
28
+ }
29
+ run ( ) {
30
+ return `${ this . name } is running at ${ this . speed } kmph` ;
31
+ }
32
+ }
33
+ // object / instance
34
+ const tommy = new Dog ( "tommy" , 3 , 45 ) ;
35
+ console . log ( tommy ) ;
36
+ console . log ( tommy . run ( ) ) ;
37
+ console . log ( tommy . eat ( ) ) ;
You can’t perform that action at this time.
0 commit comments