user1750751
user1750751

Reputation: 275

How to downcast a returned object?

I'm having problems trying downcasting on an object returned by a method. I known that isn't correct what i'm doing but I don't reach the solution.

Please see the code below.

This is a controller class where I can handle exceptions:

public class SubsystemFacade {

  public CustomClass1 method1() {
   try {
    //do something and return a new CustomClass1 object
   } catch (Exception e) {
    return (CustomClass1) this.handleExceptions(e); //Should return a CustomClass1 object but CastException is throwed
   }
  }

  public CustomClass2 method2() {
   try {
    //do something and return a new CustomClass2 object
   } catch (Exception e) {
    return (CustomClass2) this.handleExceptions(e); //Should return a CustomClass2 object but CastException is throwed
   }
  }

  private SuperCustomClass handleExceptions(Exception e) {
   //do something, log error...
   SuperCustomClass customClass = new SuperCustomClass(...);
   return customClass;
  }

}

I would like that SubsystemFacade always return an object as CustomClass1 or CustomClass2 and never an Exception.

This is my model:

public class SuperCustomClass {...}
public class CustomClass1 extends SuperCustomClass {...}
public class CustomClass2 extends SuperCustomClass {...}

Upvotes: 0

Views: 305

Answers (3)

KarlM
KarlM

Reputation: 1662

Assuming that your handleExceptions function is actually fixing the problem that causes the exceptions, then you could look at re-calling method1/method2

  private SuperCustomClass handleExceptions(Exception e, boolean callMethod1) {
   //do something, log error...
   if (callmethod1)
     return method1(); 
   return method2();
  }

Of course it is better to pass a reference to the method1/2 rather than a boolean e.g. in Java8. Or a Strategy, if you have a more complicated setup.

NB this is recursion, so be sure that your loop will actually end (i.e. if the exception rethrows you will get an infinite recursion) - fix the problem causing the exception first.

Upvotes: 1

Yassin Hajaj
Yassin Hajaj

Reputation: 21995

On the heap, when doing

SuperCustomClass customClass = new SuperCustomClass(...);

You have created an object of type SuperCustomClass using new

On the contrary, if you had done

SuperCustomClass customClass = new CustomClass1 (...);

The object on the heap would've been of type CustomClass1 even if the reference is of type SuperCustomClass

The downcasting will work if the reference can be changed to the type of the object on the heap, this is not the case here. See below.


Practicale Example

// Not possible
// Object on the heap is of type Object
Object o = new Object();
(String)o;

// Possible
// Object on the heap is of type String
Object o = new String();
(String)o;

Upvotes: 1

Nir Levy
Nir Levy

Reputation: 12953

You can't downcast an object that is SuperCustomClass to CustomClass2, cause it is not CustomClass2.

The idea in casting is that you are changing the static type (the reference type) of the object. you are not changing the actual (dynamic) type the object is.

for example, this can be casted down:

SuperCustomClass obj = new CustomClass2(...)

because the actual type is CustomClass2.

in your case, you are creating new SuperCustomClass(..) - so you can't downcast it to CustomClass2

Upvotes: 1

Related Questions