Skip to content

Commit 4f9871e

Browse files
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

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/*
22
Upcasting: Upcasting is the typecasting of a child object to a parent object.
33
4-
Upcasting can be done implicitly.Upcasting gives us the flexibility to access the parent class members,
4+
Upcasting can be done implicitly.
5+
6+
Upcasting gives us the flexibility to access the parent class members,
57
but it is not possible to access all the child class members using this feature.
68
79
Instead of all the members, we can access some specified members of the child class.
@@ -10,24 +12,20 @@ but it is not possible to access all the child class members using this feature.
1012
*/
1113

1214
class Animals {
13-
1415
String name;
1516

16-
/*A method to print the nature of the class*/
17-
void nature()
18-
{
17+
/*A method to print the nature of the class*/
18+
void nature() {
1919
System.out.println("Animal");
2020
}
2121
}
2222

2323
class Fish extends Animals {
24-
2524
String color;
26-
/*Overriding the method to print the nature of the class*/
2725

26+
/*Overriding the method to print the nature of the class*/
2827
@Override
29-
void nature()
30-
{
28+
void nature() {
3129
System.out.println("Aquatic Animal");
3230
}
3331
}
@@ -41,48 +39,50 @@ public static void main(String[] args) {
4139
Here, the Fish object is being assigned to an Animals reference.
4240
Since Fish is a subclass of Animals, this is perfectly valid.
4341
*/
42+
4443
// Parent p = new Child();
4544
Animals a = new Fish();
46-
/*
45+
46+
/*
4747
The object 'a' has access to
4848
only the parent's properties.
4949
That is, the colour property
5050
cannot be accessed from 'a'
51-
*/
52-
51+
*/
5352

5453
a.name = "GoldFish";
55-
/*
54+
55+
/*
5656
This statement throws
5757
a compile-time error
5858
a.color = "Orange";
5959
Creating an object to represent
6060
Child c = new Child();
61-
*/
62-
61+
*/
6362

6463
Fish f = new Fish();
65-
/*
64+
/*
6665
The object 'f' has access to
6766
all the parent's properties
6867
along with the child's properties.
6968
That is, the colour property can
7069
also be accessed from 'f'
71-
*/
70+
*/
7271

7372
f.name = "Whale";
7473
f.color = "Blue";
7574
/*Printing the 'a' properties*/
7675
System.out.println("Object a");
7776
System.out.println("Name: " + a.name);
7877

79-
/*
78+
79+
/*
8080
This statement will not work
8181
System.out.println("Fish1 Color" +a.color);
8282
8383
Access to child class - overridden method
8484
using parent reference
85-
*/
85+
*/
8686
a.nature();
8787

8888
/*Printing the 'f' properties*/
@@ -102,4 +102,4 @@ Animals a is a reference variable that can point to an object of type Animals or
102102
103103
So, the whole statement is demonstrating upcasting (assigning a subclass object to a superclass reference) and
104104
polymorphism, where the reference type (Animals) is more general, and the actual object created (Fish) is more specific.
105-
*/
105+
*/

0 commit comments

Comments
 (0)