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:

  1. Class P:
  2. 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.

  3. Class C:
  4. 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.

  5. Class Upcast:
  6. 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:

  1. Object1:
  2. 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.

  3. Object2:
  4. 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.

  5. Object3:
  6. 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.

2. Understanding Downcasting in Java

Downcasting is a crucial aspect of object type casting in Java, where a child class reference is assigned to a parent class. Unlike upcasting, downcasting is not implicit, and Java does not support assigning a parent class reference object directly to a child class. This process must be performed explicitly, and it comes with certain considerations.

2.1 Downcasting: Introduction and Concept

In downcasting, a parent class can be explicitly cast to a child class, allowing access to both the parent and child class properties and methods. This type of casting is necessary when certain functionalities specific to the child class need to be accessed.

2.2 Example of Downcasting

class P {
  String name;
  void m1() {
    System.out.println("This is method m1 from parent class (class P)");
  }
}

class C extends P {
  int age;
  void m1() {
    System.out.println("This is method m1 from child class (class C)");
  }
}

class Downcast {
  public static void main(String[] args) {
    P object1 = new C();
    object1.name = "Roxanne";

    // Performing Downcasting Explicitly
    C object2 = (C) object1;
    object2.age = 20;

    // Displaying all values
    System.out.println(object1.name);
    System.out.println(object2.age);
    object2.m1();

    // Note that if we try to display the below line of code:
    // object1.m1();
    // The outcome will be the same, as the method is overridden.
  }
}

In this example, we have three classes: P (parent class), C (child class), and Downcast (driver class). Let's delve into the key components and operations of this example:

  1. Object1:
  2. P object1 = new C();
    object1.name = "Roxanne";
    

    Explanation: Creating a new object (object1) of the parent class (P) using the child reference (C()). Assigning the value "Roxanne" to the string variable of object1.

  3. Object2 (Performing Downcasting Explicitly):
  4. C object2 = (C) object1;
    object2.age = 20;
    

    Explanation: Explicitly casting from the parent class to the child class. We explicitly perform the casting and inform the compiler about the runtime type of the object. This is how downcasting is performed. Now, object2 can access properties specific to the child class, such as age.

  5. Displaying Values:
  6. System.out.println(object1.name);
    System.out.println(object2.age);
    object2.m1();
    

    Explanation: Displaying the values of the string variable (name) from object1, the integer variable (age) from object2, and invoking the overridden method m1() from object2.

  7. Note on Overridden Method:
  8. // Note that if we try to display the below line of code:
    // object1.m1();
    // The outcome will be the same, as the method is overridden.
    

    Explanation: Attempting to invoke the method m1() on object1 would yield the same outcome as invoking it on object2, as the method is overridden in the child class (C).

This example showcases the explicit downcasting process and the subsequent access to child class properties and methods from a parent class reference.

2.3 Exploring the Results

Let's analyze the outcomes for each object and casting operation:

  1. Object1:
  2. P object1 = new C();
    object1.name = "Roxanne";
    

    Explanation: Creating a new object (object1) of the parent class (P) using the child reference (C()). Assigning the value "Roxanne" to the string variable of object1.

  3. Object2:
  4. C object2 = (C) object1;
    object2.age = 20;
    

    Explanation: Explicitly casting from the parent class to the child class. We explicitly perform the casting and inform the compiler about the runtime type of the object. This is how downcasting is performed.

  5. Displaying Values:
  6. System.out.println(object1.name);
    System.out.println(object2.age);
    object2.m1();
    

    Explanation: Displaying the values of the string variable (name) from object1, the integer variable (age) from object2, and invoking the overridden method m1() from object2.

Conclusion

In conclusion, downcasting provides a mechanism to access child class-specific properties and methods from a parent class reference. It involves explicit casting and is useful when certain functionalities unique to the child class are required. Understanding downcasting is essential for harnessing the full potential of polymorphism in Java.

3. Difference between Upcasting and Downcasting

To understand the differences between upcasting and downcasting, we illustrate the code as shown in diagram below.

Difference between Upcasting and Downcasting

As for the differences between both of them, we can refer to the table shown below.

Difference between Upcasting and Downcasting

End Of Article

End Of Article