Understanding Upcasting, Downcasting, and Serialization in Java

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. ...

July 23, 2024