'Converting Object of SuperClass to Sub Class

I have some Code like this.

Super Class

public class Actual {
    public int x = 123;
}

Sub classes

public class Super extends Actual{
    public int x = 999;
    public int y = 12345;
}


public class Sub extends Super {
    public int x = 144;
}

so Question is Can i convert Object of Super Class into the Sub class? is this Right for this Question what i have tried?

public class Mid1Prob1 {
    public static void main(String[] args) {
        System.out.println("Testing...");
        int x = 3;
        Actual actual = new Super();
        System.out.println(actual.x);


        Super super1 = new Super();
        System.out.println(super1.x);


        Actual act = new Actual();

        act = new Sub();
        System.out.println(act.x);
    }
}

but its giving Class Cast exception.



Solution 1:[1]

You can cast Child classes to Super classes but not vice versa. If Vehicle is Super Class and Car is a Subclass then all Cars (Child) is a Vehicle (Super) but not all Vehicle is a Car.

Solution 2:[2]

Can i convert Object of Super Class into the Sub class?

No, You can do it other way only. You can assign a sub class object to super class reference only.

Reason for it is, sub class can have more functionality and more specific to a problem. If you are allowed to case a super class object to sub class, then how that super class object will get those sub class functionality?

Solution 3:[3]

There were no possibility to do that. You can only have sth like this. Actual s = new Super(); or Super sp = new Super(); or Actual a = new Actual(); There are no other possibilities. Only that assigment might not throw classcast Exception.

Solution 4:[4]

These are the only way to create object of your super class and sub class :

Actual act = new Actual();
Actual actual = new Super();
Actual actual2 = new Sub();

Super super1 = new Super();
Super super2 = new Sub();

Sub sub = new Sub();  

There is no other way. If your tried it will give you compile time error to cast your class OR give run time error java.lang.ClassCastException.

Solution 5:[5]

Logically it's not possible because every child class has the prerogative to add properties on top of properties it has inherited from its parent, eventually leading to the different data structures. When moving upwards (casting child class to superclass) the added properties are simply ignored but the basic properties lie there due to which casting is possible. but when casting downwards (superclass to child class) the superclass simply does not fit into the child's class data structure eventually leading to class cast exceptions. Nevertheless, there is one trick that can be a little costly computing wise but it's possible to convert one class to another class (child class to superclass and vice-versa), Yes the object mapper make it possible. Below are the codes:

**SuperClass sc=new SuperClass();
sc.attribute="";
ChildClass ch= new ObjectMapper().convertValue(sc,ChildClass.class);**

This holds true even in a case where one class does not have relation to others

Solution 6:[6]

There are several situation when you need to down cast a Object. It is not possible. But you can always create a NEW subclass object using super class object. Your subclass can have a constructor which takes super class object as aurgument.

class A{

    int a = 0;
}

class B extends A{      

    int b = 0;

    public B(){

    }
    public B( A ao){            
        
        super.a = ao.a;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rogue-one
Solution 2 Abimaran Kugathasan
Solution 3
Solution 4 Yagnesh Agola
Solution 5 Dila Gurung
Solution 6 Jagannath Rao