What is casting?
Assigning one data type to another or one object to another is known as casting. Java supports two types of casting – data type casting and object casting.
Conditions of assigning objects of different classes one to another
- Same class objects can be assigned one to another and it is what we have done with Officer1 class.
- Subclass object can be assigned to a super class object and this casting is done implicitly. This is known as upcasting (upwards in the hierarchy from subclass to super class).
- strong>Java does not permit to assign a super class object to a subclass object (implicitly) and still to do so, we need explicit casting. This is known as downcasting (super class to subclass). Downcasting requires explicit conversion.
Program explaining the above rules of Java Object Casting
class Flower
{
public void smell() // I
{
System.out.println("All flowers give smell, if you can smell");
}
}
public class Rose extends Flower
{
public void smell() // II
{
System.out.println("Rose gives rosy smell");
}
public static void main(String args[])
{
Flower f = new Flower();
Rose r = new Rose();
f.smell(); // I
r.smell(); // II
f = r; // subclass to super class, it is valid
f.smell(); // II
// r = f; // super class to subclass, not valid
r = (Rose) f; // explicit casting
f.smell(); // II
}
}

Output screen of Java Object Casting
The super class Flower includes only one method smell() and it is overridden by subclass Rose.
f.smell(); // I
r.smell(); // II
In the above two statements, nothing is new as both object are calling their own smell() methods. Observe the following two statements.
f = r;
f.smell();
In the first statement, subclass object r is assigned (casted) to super class object f. It is a perfect assignment accepted by JVM and casting is done implicitly. In the super class object f, the subclass object reference (address) r exists. Calling f.smell(), f will call Rose smell() method.
From the above observation, a rule can be made as follows.
"If a subclass object is assigned to a super class object, the super class object will call subclass overridden method". This is an important rule which leads to dynamic polymorphism.
// r = f;
The above statement does not compile as super class object is assigned to subclass object. To do it, we require explicit casting.
r = (Rose) f; // explicit casting
f.smell(); // II
In the above statement, Flower object f is explicitly casted to Rose object r. In the earlier statement f = r, the f includes Rose reference. Now, f returns the Rose reference to Rose itself. For this reason f.smell() prints the subclass Rose smell(). That is, r is calling its own smell(). What happens if f = r does not exist earlier. It will be a an exception (not compilation error). That is, "before doing explicit casting, implicit casting must be done".
can you explain what is the Benifit of Object Level Type Casting
Sir since a holds reference of b initailly right .then what is the need of type casting again
if suppose i need to call again the method of super class how to do tat?
the 27 line ll fetch the content of rose class.
You cannot, but only way is assign with another super class object.
r = (Rose) f;
f.smell();
How could it run fine? This is not possible. Run it yourself and try once.
I am getting this exception.
Exception in thread “main” java.lang.ClassCastException: Flower cannot be cast t
o Rose
at Rose.main(Rose.java:17)
Before doing this, do f = r;
What is the advantage, if we create a subclass object and assigned it to a super class reference?
It is how Java achieves dynamic polymorphism.
in 27th line why did you put f.smell(); instead of r. smell.
because you assigned super class object to subclass object in 26th line then why did you call smell() method on super class object?
please could you clearly explain that?
Thank you
It is to show that super class object Flower will call subclass Rose smell() method.
The idea of the program is to show when a subclass object is assigned to super class object, the super class object will call subclass method.
Thank you and i understood the idea further when i looked the 2nd page.
Thank you for your quick reply.
That was very helpful . Thank You
before doing explicit casting, implicit casting must be done” – why this throws an exception?
In Java explicit casting does not come free of cost. Before doing explicit casting, you must store the subclass object in the super class object. Once stored, super class object returns sub class object (which you have stored earlier).
Imagine, I want you to give me a red marker. Unless I gave earlier, how you will give to me? The same thing here also.
could u please clarify this ” JVM keeps a subclass object in object of Object class and then returns “
many methods Java API do not know what to run until runtime. These methods know what to return dynamically. For example, the firstElement() method of Vector returns the fist element stored. How to design the method firstElement(); Vector do not know what the element the user stores. To overcome this problem, the designers preferred to return Object class like this:
public Object firstElement();
Suppose, if the user stores an Integer object as the first element, then what happens let us see.
The JVM does like this:
Integer i1 = new Integer(10);
Object obj1 = i1; // implicit casting
return obj1;
i1 subclass object is stored in super class object obj1 of Object class and then returned.
While retrieving what you will do?
Object obj2 = vect.firstElement();
Integer i1 = (Integer) obj2; // explicit casting
Hi Sir,
Could you please elaborate that what happens inside with the statement:
r = (Rose) f;
Thank You
Abhishek Rajan.
Object f returns the address of Rose stored earlier.
The Rose object stored earlier in Flower object f is simply returned to Rose object r.
sir,is downcasting or upcasting possible when there is ‘is-A’ relation?
Up casting or down casting is possible in Java only when “is-a relationship” (achieved with inheritance) exists.
sir, how many ways are there to create an object in java?
1. By using new keyword
2. Using Class.forName(): Refer: http://way2java.com/java-lang/creating-object-without-new-keyword/
3. Using Cloning
4. Using factory method
After f=r Statement we can directly call child method.. In this example what is the advantage of object casting?
Y can’t I do explicit casting without implicit casting..
In implicit casting, we place the reference of a subclass object in super class object. In explicit casting, we get back the subclass reference object from super class object. Why all this twist? Observe, many methods of Java API, return an object of Object class. JVM keeps a subclass object in object of Object class and then returns; because it does not know what object it gets at runtime to return. We do explicit casting and get back the object. For example, the elementAt(int) method of Vector class.
what is type costting?it is used where and why?
Casting is converting one data type to other OR converting one object to other. Suppose you entered an int value which you read into the program. But the program requires as short or long data type. Then what you will do? You cast.
Not possible.
Thank you sir for clearing my concept