1. Upcasting: Introduction and Concept
Typecasting in Java is a process that involves converting one data type to another, and this concept extends beyond primitive data types to objects. In particular, we'll focus on two types of object typecasting: upcasting and downcasting. This understanding is essential as it forms the basis for applying polymorphism in Java.
Upcasting is a form of object typecasting where a child object is cast to a parent class object. This enables access to the variables and methods of the parent class within the child class. Upcasting, also known as Generalization and Widening, is a crucial concept for polymorphism.
1.1 Example of Upcasting
class P {
void m1() {
System.out.println("This is method m1 from parent class (class P)");
}
}
class C extends P {
void m1() {
System.out.println("This is method m1 from child class (class C)");
}
}
class Upcast {
public static void main(String[] args) {
P object1 = new P();
object1.m1();
P object2 = new C();
object2.m1();
C object3 = new C();
object3.m1();
}
}
In this example, we have two classes, P (parent class) and C (child class), along with a driver class called Upcast. Let's break down the key components of this example:
- Class P:
- Class C:
- Class Upcast:
class P {
void m1() {
System.out.println("This is method m1 from parent class (class P)");
}
}
Class P defines a method m1() that prints a message indicating that it is from the parent class.
class C extends P {
void m1() {
System.out.println("This is method m1 from child class (class C)");
}
}
Class C is a child class of P and overrides the m1() method to print a message specific to the child class.
class Upcast {
public static void main(String[] args) {
P object1 = new P();
object1.m1();
P object2 = new C();
object2.m1();
C object3 = new C();
object3.m1();
}
}
Class Upcast contains the main() method where instances of classes P and C are created and their m1() methods are invoked. This demonstrates the concept of upcasting, where a child object is typecasted to a parent object.
The output of the program showcases how upcasting allows the invocation of the appropriate m1() method based on the actual type of the object at runtime, fostering dynamic polymorphism.
1.2 Exploring the Results
Let's analyze the outcomes for each object creation:
- Object1:
- Object2:
- Object3:
P object1 = new P();
object1.m1();
Output: "This is method m1 from parent class (class P)"
Explanation: Creating a new object (object1) of the parent class (P) and calling method m1() from the created object.
P object2 = new C();
object2.m1();
Output: "This is method m1 from child class (class C)"
Explanation: Creating a new object (object2) of the parent class (P) using the child reference (C()). This is upcasting, where the child object is typecasted to a parent object.
C object3 = new C();
object3.m1();
Output: "This is method m1 from parent class (class P)"
Explanation: Creating a new object (object3) of the child class (C) using the child reference (C()). Calling method m1() from the created object.
Conclusion
In conclusion, upcasting allows for dynamic polymorphism by treating a child object as an instance of its parent class. This concept is instrumental in achieving flexibility and reusability in Java code. Understanding upcasting is a crucial stepping stone for diving deeper into the world of polymorphism.