Object Casting Java

Object casting with Interface (getting interface objects)

With abstract classes and interfaces, objects cannot be created, but reference variables can be created. The above program is modified to suit interfaces. Here, Flower is an interface (and it can be abstract class also).

interface Flower
{
  public abstract void smell();
}
public class Rose implements Flower
{
  public void smell()
  {
    System.out.println("Rose gives rosy smell");
  }
   public static void main(String args[])
   {
     Flower f;
     Rose r = new Rose();
			
     r.smell();		                   // II

     f = r;                                // subclass object to super class reference variable
     f.smell();		                   // II

  // r  = f;                               // super class to subclass, not valid
     r = (Rose) f;	                   // explicit casting
     f.smell();		                   // II
   }
}

Java Object Casting

             
  Flower f;
  f = r;           // subclass object to super class reference variable
  f.smell();	   // II

The subclass object r is assigned to super class reference variable f. Here, f is a reference variable of interface Flower; but for this, the program is the same as earlier. When assigned, the reference variable works practically as an object. Like this, we can get the objects of interfaces and abstract classes, but we cannot create objects.

Learn more on Object casting problems.

30 thoughts on “Object Casting Java”

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

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

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

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

  3. Hi Sir,

    Could you please elaborate that what happens inside with the statement:

    r = (Rose) f;

    Thank You
    Abhishek Rajan.

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

Leave a Comment

Your email address will not be published.