Commit 4f9871e
committed
feat: Implement detailed demonstration of Upcasting with method overriding in Java
This commit adds a complete Java program that demonstrates the concept of upcasting
(child to parent class type conversion), object access limitations, and method overriding.
Key components:
- Defined a parent class `Animals` with a `name` property and a method `nature()`
- Created a child class `Fish` extending `Animals`, adding a `color` property
and overriding the `nature()` method to reflect aquatic behavior
- In `main`, demonstrated:
- Upcasting: `Animals a = new Fish();` — a `Fish` object referenced by a `Animals` type
- Access limitation: `a` can only access fields and methods defined in `Animals`, not `Fish`-specific ones
- Method overriding: `a.nature()` calls the overridden method in `Fish` due to runtime polymorphism
- Direct child object usage: `Fish f = new Fish();` can access both `name` and `color`, showing full class access
- Included explanatory comments throughout the code to clarify:
- Differences between upcasting and direct object reference
- Compile-time restrictions and runtime behavior
- Use cases for overriding and polymorphic method invocation
This example provides a foundational understanding of inheritance, typecasting,
method overriding, and polymorphism in Java — crucial concepts for object-oriented design.
Signed-off-by: Somesh diwan <[email protected]>1 parent 1ebf85e commit 4f9871e
1 file changed
+20
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
| |||
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
13 | | - | |
14 | 15 | | |
15 | 16 | | |
16 | | - | |
17 | | - | |
18 | | - | |
| 17 | + | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
25 | 24 | | |
26 | | - | |
27 | 25 | | |
| 26 | + | |
28 | 27 | | |
29 | | - | |
30 | | - | |
| 28 | + | |
31 | 29 | | |
32 | 30 | | |
33 | 31 | | |
| |||
41 | 39 | | |
42 | 40 | | |
43 | 41 | | |
| 42 | + | |
44 | 43 | | |
45 | 44 | | |
46 | | - | |
| 45 | + | |
| 46 | + | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
52 | | - | |
| 51 | + | |
53 | 52 | | |
54 | 53 | | |
55 | | - | |
| 54 | + | |
| 55 | + | |
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
62 | | - | |
| 61 | + | |
63 | 62 | | |
64 | 63 | | |
65 | | - | |
| 64 | + | |
66 | 65 | | |
67 | 66 | | |
68 | 67 | | |
69 | 68 | | |
70 | 69 | | |
71 | | - | |
| 70 | + | |
72 | 71 | | |
73 | 72 | | |
74 | 73 | | |
75 | 74 | | |
76 | 75 | | |
77 | 76 | | |
78 | 77 | | |
79 | | - | |
| 78 | + | |
| 79 | + | |
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | | - | |
| 85 | + | |
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | | - | |
| 105 | + | |
0 commit comments