-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance in Java DEEPDIVEmd
More file actions
2234 lines (1609 loc) · 31.8 KB
/
Copy pathInheritance in Java DEEPDIVEmd
File metadata and controls
2234 lines (1609 loc) · 31.8 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Inheritance in Java — DEEPDIVE
Inheritance is one of the most important concepts in **OOPs (Object-Oriented Programming)**. It allows one class to acquire the accessible properties and methods of another class.
The main purpose of inheritance is **code reusability** and creating an **IS-A relationship** between classes.
---
# 1. What is Inheritance?
### Definition
> **Inheritance is a mechanism in Java by which one class acquires the accessible properties and methods of another class.**
The class whose members are inherited is called the:
* **Parent Class**
* **Super Class**
* **Base Class**
The class that inherits those members is called the:
* **Child Class**
* **Sub Class**
* **Derived Class**
### Syntax
```java
class Child extends Parent
{
// child class members
}
```
The `extends` keyword establishes the inheritance relationship between two classes.
---
# 2. Why Do We Need Inheritance?
Suppose we have three types of robots:
```text
Fighter Robo
Player Robo
Teacher Robo
```
All three robots have some common activities:
```text
move()
learn()
recharge()
interact()
```
Without inheritance, we would have to write the same methods in all three classes.
That creates **duplicate code**.
Instead, we create one common parent:
```text
Robo
|
┌────────────┼────────────┐
↓ ↓ ↓
FighterRobo PlayerRobo TeacherRobo
```
Common methods are placed inside `Robo`.
Specific methods are placed inside the child classes.
```text
Robo
├── move()
├── learn()
├── recharge()
└── interact()
FighterRobo
└── fight()
PlayerRobo
└── play()
TeacherRobo
└── teach()
```
This is **code reusability through inheritance**.
---
# 3. Robot Example — Complete Inheritance Program
This is the basic program structure we will use throughout the explanation.
```java
class Robo
{
public void move()
{
System.out.println("Robo moves fast");
}
public void learn()
{
System.out.println("Robo self learns");
}
public void recharge()
{
System.out.println("Plug in to recharge");
}
public void interact()
{
System.out.println("Robo interacts");
}
}
class FighterRobo extends Robo
{
public void fight()
{
System.out.println("Robo fights");
}
}
class PlayerRobo extends Robo
{
public void play()
{
System.out.println("Robo plays games");
}
}
class TeacherRobo extends Robo
{
public void teach()
{
System.out.println("Robo teaches");
}
}
class RoboApp
{
public static void main(String[] args)
{
FighterRobo fr = new FighterRobo();
System.out.println("Fighter Robo");
fr.move();
fr.learn();
fr.recharge();
fr.interact();
fr.fight();
System.out.println("----------------");
PlayerRobo pr = new PlayerRobo();
System.out.println("Player Robo");
pr.move();
pr.learn();
pr.recharge();
pr.interact();
pr.play();
System.out.println("----------------");
TeacherRobo tr = new TeacherRobo();
System.out.println("Teacher Robo");
tr.move();
tr.learn();
tr.recharge();
tr.interact();
tr.teach();
}
}
```
---
# 4. Understanding the Program Step-by-Step
## Step 1: Parent Class
```java
class Robo
```
`Robo` is the parent class.
It contains four common methods:
```java
move()
learn()
recharge()
interact()
```
These methods represent common behavior of every robot.
---
## Step 2: FighterRobo
```java
class FighterRobo extends Robo
```
This means:
> `FighterRobo` is a child class of `Robo`.
Therefore, a `FighterRobo` object can access the accessible methods of `Robo`.
It additionally has its own method:
```java
fight()
```
So its accessible methods are:
```text
move()
learn()
recharge()
interact()
fight()
```
---
## Step 3: PlayerRobo
```java
class PlayerRobo extends Robo
```
It inherits:
```text
move()
learn()
recharge()
interact()
```
and has its own:
```text
play()
```
Therefore:
```text
PlayerRobo
├── move()
├── learn()
├── recharge()
├── interact()
└── play()
```
---
## Step 4: TeacherRobo
```java
class TeacherRobo extends Robo
```
It inherits:
```text
move()
learn()
recharge()
interact()
```
and has its own:
```text
teach()
```
Therefore:
```text
TeacherRobo
├── move()
├── learn()
├── recharge()
├── interact()
└── teach()
```
---
# 5. What Happens When We Create an Object?
Consider:
```java
FighterRobo fr = new FighterRobo();
```
Here:
```text
FighterRobo
↓
new FighterRobo()
↓
FighterRobo object
```
The object can access the methods available through its class hierarchy.
Conceptually:
```text
FighterRobo Object
|
┌────────────┼────────────┐
↓ ↓ ↓
Robo FighterRobo inherited
methods method methods
```
So:
```java
fr.move();
fr.learn();
fr.recharge();
fr.interact();
fr.fight();
```
are valid.
---
# 6. Why Can the Child Access Parent Methods?
Because:
```java
class FighterRobo extends Robo
```
creates an inheritance relationship.
Therefore:
```text
FighterRobo IS-A Robo
```
This is called an **IS-A relationship**.
Examples:
```text
FighterRobo IS-A Robo
PlayerRobo IS-A Robo
TeacherRobo IS-A Robo
```
Similarly:
```text
Dog IS-A Animal
Car IS-A Vehicle
Manager IS-A Employee
```
---
# 7. Inheritance and Code Reusability
### Without inheritance
Suppose every robot has:
```java
public void move()
{
System.out.println("Robo moves fast");
}
```
We would need to write it three times.
```text
FighterRobo → move()
PlayerRobo → move()
TeacherRobo → move()
```
### With inheritance
Write it only once:
```java
class Robo
{
public void move()
{
System.out.println("Robo moves fast");
}
}
```
Then:
```java
class FighterRobo extends Robo
{
}
```
```java
class PlayerRobo extends Robo
{
}
```
```java
class TeacherRobo extends Robo
{
}
```
All three can reuse `move()`.
### Therefore:
> **Inheritance reduces code duplication and promotes code reusability.**
---
# 8. Types of Inheritance in Java
There are several inheritance structures that are important for Java.
1. Single Inheritance
2. Hierarchical Inheritance
3. Multilevel Inheritance
4. Hybrid Inheritance
5. Multiple Inheritance — not supported through classes
Let's understand each carefully.
---
# 9. Single Inheritance
### Definition
> When one child class inherits from one parent class, it is called **Single Inheritance**.
### Diagram
```text
Parent
↓
Child
```
### Example
```java
class Animal
{
public void eat()
{
System.out.println("Animal eats");
}
}
class Dog extends Animal
{
public void bark()
{
System.out.println("Dog barks");
}
}
class AnimalApp
{
public static void main(String[] args)
{
Dog d = new Dog();
d.eat();
d.bark();
}
}
```
### Output
```text
Animal eats
Dog barks
```
`Dog` gets `eat()` from `Animal` and has its own `bark()`.
---
# 10. Hierarchical Inheritance
### Definition
> When **one parent class is extended by multiple child classes**, it is called Hierarchical Inheritance.
### Diagram
```text
Parent
/ \
/ \
Child-1 Child-2
```
If there are three children:
```text
Robo
/ | \
↓ ↓ ↓
Fighter Player Teacher
```
Our Robot example is therefore **Hierarchical Inheritance**.
### Program
```java
class Robo
{
public void move()
{
System.out.println("Robo moves fast");
}
public void learn()
{
System.out.println("Robo self learns");
}
public void recharge()
{
System.out.println("Plug in to recharge");
}
public void interact()
{
System.out.println("Robo interacts");
}
}
class FighterRobo extends Robo
{
public void fight()
{
System.out.println("Robo fights");
}
}
class PlayerRobo extends Robo
{
public void play()
{
System.out.println("Robo plays games");
}
}
class TeacherRobo extends Robo
{
public void teach()
{
System.out.println("Robo teaches");
}
}
```
---
# 11. Important Point in Hierarchical Inheritance
`FighterRobo` does **not** inherit `play()`.
Why?
Because:
```text
play()
```
belongs to `PlayerRobo`.
Similarly:
```text
fight()
```
belongs only to `FighterRobo`.
And:
```text
teach()
```
belongs only to `TeacherRobo`.
The common methods belong to `Robo`.
```text
Robo
move()
learn()
recharge()
interact()
|
┌──────────┼──────────┐
↓ ↓ ↓
Fighter Player Teacher
fight() play() teach()
```
---
# 12. Multilevel Inheritance
### Definition
> When inheritance occurs at multiple levels, it is called Multilevel Inheritance.
For the three-class structure you specified:
```text
Grand Parent
↓
Parent
↓
Child
```
### Example
```java
class Animal
{
public void eat()
{
System.out.println("Animal eats");
}
}
class Dog extends Animal
{
public void bark()
{
System.out.println("Dog barks");
}
}
class Puppy extends Dog
{
public void play()
{
System.out.println("Puppy plays");
}
}
```
### Object
```java
Puppy p = new Puppy();
```
The `Puppy` object can access:
```text
eat() → Animal
bark() → Dog
play() → Puppy
```
Conceptually:
```text
Animal
|
| eat()
↓
Dog
|
| bark()
↓
Puppy
|
| play()
```
---
# 13. How Multilevel Inheritance Works
Consider:
```java
Puppy p = new Puppy();
```
Then:
```java
p.eat();
```
`eat()` is not declared directly inside `Puppy`.
It is declared in `Animal`.
But because:
```text
Puppy → Dog → Animal
```
the method can be accessed through the inheritance hierarchy.
Similarly:
```java
p.bark();
```
comes from `Dog`.
And:
```java
p.play();
```
comes from `Puppy`.
---
# 14. Hybrid Inheritance
### Definition
> Hybrid inheritance is a combination of more than one inheritance structure.
Your requested example is:
```text
Animal
/ | \
↓ ↓ ↓
Herbivores Carnivores Omnivores
↓ ↓ ↓
Cow Tiger Dog
```
Here:
```text
Animal → Herbivores → Cow
```
is a multilevel path.
Similarly:
```text
Animal → Carnivores → Tiger
```
and:
```text
Animal → Omnivores → Dog
```
The three branches from `Animal` demonstrate a hierarchical structure, while each branch has another level.
So the overall structure combines **hierarchical and multilevel inheritance**.
---
# 15. Hybrid Inheritance Program
```java
class Animal
{
public void eat()
{
System.out.println("Animal eats");
}
}
class Herbivores extends Animal
{
public void grass()
{
System.out.println("Herbivores eat grass");
}
}
class Carnivores extends Animal
{
public void meat()
{
System.out.println("Carnivores eat meat");
}
}
class Omnivores extends Animal
{
public void both()
{
System.out.println("Omnivores eat plants and meat");
}
}
class Cow extends Herbivores
{
public void giveMilk()
{
System.out.println("Cow gives milk");
}
}
class Tiger extends Carnivores
{
public void hunt()
{
System.out.println("Tiger hunts");
}
}
class Dog extends Omnivores
{
public void bark()
{
System.out.println("Dog barks");
}
}
```
### Structure
```text
Animal
/ | \
↓ ↓ ↓
Herbivores Carnivores Omnivores
↓ ↓ ↓
Cow Tiger Dog
```
For example:
```java
Cow c = new Cow();
c.eat();
c.grass();
c.giveMilk();
```
The `Cow` object can access:
```text
eat() → Animal
grass() → Herbivores
giveMilk() → Cow
```
---
# 16. Multiple Inheritance
### Meaning
Multiple inheritance means:
> One child class directly inherits from more than one parent class.
For example:
```text
A B
\ /
\ /
C
```
If Java allowed this with classes, we could write conceptually:
```text
class C extends A, B
```
But **Java does not support multiple inheritance through classes**.
---
# 17. Why Doesn't Java Support Multiple Inheritance Through Classes?
The major reason is the **Diamond Problem**.
Consider:
```text
A
/ \
B C
\ /
D
```
Suppose `A` has:
```java
public void show()
{
System.out.println("A");
}
```
Suppose both `B` and `C` provide their own `show()`.
Then `D` receives two possible versions.
If we write:
```java
D d = new D();
d.show();
```
Which method should execute?
```text
B's show()
OR
C's show()
```
This creates ambiguity.
Therefore Java avoids this problem by **not allowing multiple inheritance through classes**.
---
# 18. Diamond Problem Program — Conceptual Example
The problematic structure would be:
```text
Robo
/ \
A B
\ /
C
```
Suppose:
```java
class Robo
{