In Java, Casting means conversion of some thing automatically or manually to the desired thing.
Lets understand Casting with examples -
Automatic Casting by Java -
Converting integer value to a long type as shown below
long var1 = 5; //Automatic Conversion of int to long type
In the above statement, the integer value 5 will be converted automatically to long type value and assigned to the long type variable 'var1'.
But when you assign a long type value to an integer type variable, the compiler will throw errors as the assigned value is not automatically converted to the int type. In order to assign the long type value to the int type variable, we have to manually cast the long type value to the int type as shown below.
Manual Casting by Java -
Manually converting long type value to an int type as shown below -
int var1 = (int) 92273372036854775807L; //Manual Conversion
In the above statement, the long type value 92273372036854775807L will be converted manually to int type value and assigned to int type variable 'var1'. (int) code before the long type value will manually cover the long type value to the int type value.
The same applies for objects also, while assigning an object reference of any class type to an object of another class type.
What is Upcasting ?
Upcasting is the process of assigning the Sub Class object reference to the Super Class object as shown below -
Lets assume that ClassTwo is the subclass of ClassOne.
ClassOne object1 = new ClassOne( );
ClassTwo object2 = new ClassTwo( );
object1 = object2; //Upcasting
In the above statement we have assigned the sub class object reference 'object2' to the super class object 'object1'. This is called as Upcasting (i.e. object2 is upcasted to object1).
While Upcasting an object, Java will automatically convert the assigned object of the sub class to the super class object type. No manual conversion is required.
Upcasting any Class object to Object Class object -
As all the Classes in Java are the sub classes of the Object Class, we can upcast object of any Class type to the object of Object Class as shown below -
ClassOne object1 = new ClassOne( );.
Object object = object1; //Upcasting to Object Class object
Why Java automatically upcasts a sub class object to super class object ?
Lets assume that Animal Class is the super class of Dog and Cat Classes as shown below -
From the above screenshot Upcasting is automatically done i.e. Cat's object is automatically upcasted to Animal's object because Cat is an Animal. Hence all the sub classes can be automatically upcasted to super class object.
First, you must understand that by casting you are not actually changing the object itself, you are just labeling it different. For example if you upcast the Cat Class object to Animal Class object, then the object doesn't stop from being a Cat. It's still a Cat, but it's just labelled as an Animal with Animal Properties.
Using Cat's objects we can access the members of Cat Class. But when we assign the Cat's object to the Animal Object as shown below -
Animal animal = new Cat( );
Using animal object we can access only the members (i.e. instance variables and methods) of Animal Class along with the members of Cat's Class which are inherited from the Animal Class only. We cant access the members of Cat's Class which are not inherited from the Animal Class using the animal object. In order to access the Cat's Class specific members, we have to use Cat's Class object.
Please comment below to feedback or ask questions.