Object Casting Problems Java

Every language comes with data type casting and in Java, also exist object casting. For a novice, it is very confusing. Let us discuss different variations of Object Casting Problems.

Before going though this, if you are not aware, it is advised to read Object Casting.

Java supports object casting but with some restrictions.

Case 1 of Object Casting Problems: Casting between objects not involved in inheritance

Java does not support casting (assignment) between the objects of different classes which are not involved in inheritance. In the following program, Animal and Dog classes are no way connected; there is no inheritance. Compiler raises error "incompatible types".

class Animal
{
  public void eat()
  {
    System.out.println("Animal eats");
  }
}
public class Dog
{
  public void eat()
  {
    System.out.println("Dog eats");
  }
  public static void main(String args[])
  {
     Animal a1 = new Animal();
     Dog d1 = new Dog();
     a1 = d1;
     a1.eat();
  }
}


Object Casting Problems

Now let us see the casting between objects involved in inheritance.

Case 2 of Object Casting Problems: Super class object is assigned to subclass object

Java does not permit to assign a super class object to a subclass object (requires extra explicit casting operation). It is known as down casting (in the inheritance diagram, super class is on the top and its down is subclass). Compiler throws "incompatible types".

class Animal
{
  public void eat()
  {
    System.out.println("Animal eats");
  }
}
public class Dog extends Animal
{
  public void eat()
  {
    System.out.println("Dog eats");
  }
  public static void main(String args[])
  {
     Animal a1 = new Animal();
     Dog d1 = new Dog();
     d1 = a1;
     a1.eat();
  }
}


Object Casting Problems

Observe, in the above code, subclass Dog object d1 is assigned with super class Animal object a1. It is erroneous.

Case 3 of Object Casting Problems: Sub class object is assigned to a super class object

Java permits happily to assign a subclass object to a super class object and the casting is done implicitly. It is known as up casting (in the inheritance diagram, super class is up of subclass).

class Animal
{
  public void eat()
  {
    System.out.println("Animal eats");
  }
}
public class Dog extends Animal
{
  public void eat()
  {
    System.out.println("Dog eats");
  }
  public static void main(String args[])
  {
     Animal a1 = new Animal();
     Dog d1 = new Dog();
     a1 = d1;
     a1.eat();
  }
}


Object Casting Problems

In the above code, when d1 is assigned to a1, the Animal class calls subclass overridden method. This rule is clearly discussed earlier in Object casting.

Case 4 of Object Casting Problems: Casting to a wrong object – "Inconvertible types"

We have seen earlier "Incompatible types" and now let us see "Inconvertible types" "Inconvertible types" occurs when wrong object is cast. See the following code.

class Animal   {    }

class Cat   {    }

public class Dog extends Animal
{
  public static void main(String args[])
  {
    Animal a1 = new Animal();
    Dog d1 = new Dog();
    Cat c1 = new Cat();

    a1 = d1;   
    c1 = (Cat) a1;
  }
}


Object Casting Problems

a1 = d1;
c1 = (Cat) a1;

In Animal object a1, reference of Dog object d1 exists. In the next statement, a1 object (having Dog reference) is casted to Cat. Animal object is inconvertible to Cat.

Case 5: Casting to a wrong object with predefined classes – "ClassCastException"

In the following code, The object obj of Object class is having the reference of Integer. But is casted to String explicitly. Program compiles successfully but JRE raises "ClassCastException">. See the output screen.

public class Demo
{
  public static void main(String args[])
  {
    Integer i1 = new Integer(10);
    Object obj = i1;
    String str = (String) obj;    
  }
}

Object Casting Problems

4 thoughts on “Object Casting Problems Java”

  1. In Case 5:
    Can we achieve like this?
    public class Demo {
    public static void main(String args[]) {
    Integer i1 = new Integer(10);
    Object obj = i1;
    String str = null;
    obj=str;
    str = (String) obj;
    }
    }

Leave a Comment

Your email address will not be published.