sneppets-java8

Casting Object Data Types in Java

This tutorial guides you on how to cast object data types in Java programming. In this tutorial you will learn how to convert sub class object to super class type and super class type in to sub class type by casting object data types.

Casting object data types – Convert subclass object type to superclass

When you want to convert subclass object type to superclass type, it can be done by using super class reference to refer to sub class object and performing type casting with super class type as shown in the following example sup= (Super)new Sub() .

class Super
{
  void method1(){
    System.out.println("In Super");
  }
}

class Sub extends Super
{
  void method2(){
    System.out.println("In Sub");
  }
}

class Cast
{
  public static void main(String args[]){

     Super sup;  //sup -> super class reference
     //casting sub class object data type to super class type
     sup= (Super)new Sub();  //sup refer to sub class object, 
     sup.method1();

  }
}

Output

In Super

Note, in the above case we will be able to call method1() of super class, but we won’t be able to call the method2() of sub class. If you try to call method2() it would result in compile time error.

Therefore, in the above case, the programmer will be able to access all the methods of super class, but not the sub class methods. We can say programmer has access to only 50% of the functionalities.

Suppose, if we override the super class methods in the sub class as shown in the example below, then it is possible to access sub class methods but not the super class methods.

In either cases programmer will get only 50% functionality.

class Super
{
  void method1(){
    System.out.println("In Super");
  }
}

class Sub extends Super
{
  void method1(){  //override the super class method
    System.out.println("In Sub");
  }
}

class Cast
{
  public static void main(String args[]){

     Super sup;  //sup -> super class reference
     //casting sub class object data type to super class type
     sup= (Super)new Sub();  //sup refer to sub class object
     sup.method1();

  }
}

Output

In Sub

Casting object data types – Convert superclass data type to sub class type

When you want to convert superclass data type to subclass type, it can be done by taking sub class reference to refer to super class object and performing type casting with sub class type as shown in the following example sub= (Sub)new Super() .

class Super
{
  void method1(){
    System.out.println("In Super");
  }
}

class Sub extends Super
{
  void method2(){
    System.out.println("In Sub");
  }
}

class Cast
{
  public static void main(String args[]){

     Sub sub;  //sub -> sub class reference
     //casting super class object data type to sub class type 
     sub= (Sub)new Super();  //sub refer to super class object
     sub.method1();

  }
}

Output

Exception in thread "main" java.lang.ClassCastException:
Super cannot be cast to Sub

Therefore, while converting super class type to sub class type, if you use super class object we cannot access any methods of super class or even sub class.

Try sub.method2(), that will also result in same error. So programmer will get 0% functionality in this case.

The solution to the above problem is try to create an object to sub class instead of creating an object to super class as shown in the following example.

class Super
{
  void method1(){
    System.out.println("In Super");
  }
}

class Sub extends Super
{
  void method2(){
    System.out.println("In Sub");
  }
}

class Cast
{
  public static void main(String args[]){

     //convert super class type to sub class type
     Super sup; //sup -> super class reference

     //create an object to sub class
     sup = new Sub(); //sup -> super class reference refer to sub class object

     //casting super class reference referring to sub class object with sub class type
     Sub sub = (Sub)sup; 
 
     //access super class and sub class methods - 100% functionality access
     sub.method1();
     sub.method2();

  }
}

Output

In Super
In Sub

It is evident that if an object to sub class is created, then it is possible to access all the methods of super class and sub class. 100% functionality accessibility is acheived.

Note, this is the reason why in inheritance we always create an object to sub class, but not to super class.

Hope this tutorial helped you to understand the concepts of casting objects data types :-).

Also See:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments