Overview

This note covers the concepts of upcasting, downcasting, and serialization in Java. It explains what happens to object fields during these processes and includes a practical example.

Classes Definition

Let's define two classes, Alpha and Beta, where Beta is a subclass of Alpha.

class Alpha {
    int a1;
    int a2;
    int a3;
}

class Beta extends Alpha {
    int b4;
}

Upcasting

When you upcast an object of a subclass (Beta) to its superclass (Alpha), you are changing the reference type but not the actual object in memory.

Beta beta = new Beta();
Alpha alpha = (Alpha) beta; // Upcasting Beta to Alpha

Example

public class Test {
    public static void main(String[] args) {
        Beta beta = new Beta();
        Alpha alpha = (Alpha) beta; // Upcasting Beta to Alpha

        // Accessing fields through alpha reference
        alpha.a1 = 10; // This is allowed
        alpha.a2 = 20; // This is allowed
        alpha.a3 = 30; // This is allowed

        // The following line would cause a compile-time error
        // alpha.b4 = 40; // Not allowed, b4 is not defined in Alpha
    }
}

Downcasting

When you downcast a reference from a superclass to a subclass, you can access the subclass-specific fields and methods.

Beta betaAgain = (Beta) alpha; // Downcasting Alpha to Beta
betaAgain.b4 = 40; // This is allowed

Example

public class Test {
    public static void main(String[] args) {
        Beta beta = new Beta();
        Alpha alpha = (Alpha) beta; // Upcasting Beta to Alpha

        // Downcasting alpha back to Beta
        Beta betaAgain = (Beta) alpha; // Downcasting Alpha to Beta

        // Now you can access the b4 field
        betaAgain.b4 = 40; // This is allowed

        // Verifying access to all fields
        System.out.println("a1: " + betaAgain.a1);
        System.out.println("a2: " + betaAgain.a2);
        System.out.println("a3: " + betaAgain.a3);
        System.out.println("b4: " + betaAgain.b4);
    }
}

Serialization

Serialization is the process of converting an object's state into a byte stream. All fields of the actual object are included in the serialized form, regardless of the reference type.