0

I basically have the same problem that was described here Invalid conversion from runtime type MyClass.A to MyClass.B but i'm unclear as to what the solution is

I have a virtual class A and classes B and C that both extend A. A has some fields, B and C do nothing have anything in them and are only used for the class name.

I have a method which return an A object. I then want to cast this object into a B and a C. Why am i getting this error?

example code

public virtual class A {
   //fields
}

public class B extends A {
   //nothing
}

public A someMethod(){
   return new A();
}

public void myMethod(){
   B b1 = (B) someMethod();
}

EDIT: apparently the solution is to create another helper method that will take a parent object as an argument. You create a child object, modify that object in your method (you can modfy both chil objects in the same way), return it as an object and then cast that object back into a child. Idk why but that worked.

3
  • 2
    Please edit this post to include your specific code.
    – Adrian Larson
    Feb 22, 2019 at 15:59
  • @AdrianLarson done
    – Ben Arnao
    Feb 22, 2019 at 16:07
  • I am afraid it might be a X-Y problem due to most recent edits (xyproblem.info). Can you elaborate in details what is the scenario of usage of A, B and C and why do you want to cast A to B?
    – kurunve
    Feb 22, 2019 at 17:23

2 Answers 2

2

This is the expected behavior for any Object Oriented Programming.

What you have currently returning from your method is the instance of the Parent Class, and thus it cannot be cast to an instance of Child Class.

public A someMethod(){
   return new A();
}

Only if you had your method something as below, it would have then worked.

public A someMethod(){
   return new B(); // returns instance of B
}
8
  • Is there any way to do what i am trying to do then without creating two blocks of code for all the fields of class A?
    – Ben Arnao
    Feb 22, 2019 at 16:34
  • You can return the parent type, but if you need to cast it to a child instance, you need to make sure that you create an instance of the child itself. While I am not completely sure what are you trying to achieve, you can always determine based on a parameter as which type of instance you need to create - parent or child.
    – Jayant Das
    Feb 22, 2019 at 16:37
  • basically i have two classes that have the same attributes but different class names and i want to write as little code as possible so i want a parent cass with all the details and child classes with different names and treat them similarly until the end where i make them into their child classes
    – Ben Arnao
    Feb 22, 2019 at 16:41
  • Why do you need two classes then if all attributes are same? I would recommend you revisit your design here. There's no way that you can cast an instance of parent to child, that's simply not allowed.
    – Jayant Das
    Feb 22, 2019 at 16:44
  • it's because i have a model that on of my swagger classes converts to json so i need the class names to match the expected json format
    – Ben Arnao
    Feb 22, 2019 at 16:45
2

This is class A

Class A

This is Class B and Class C they Both extends class A

Class B and Class C

Now when you try to put an instance of Class C in Class A, you can as C has A in it, so

But When you try other way round, Trying to Assign Instance of Class A in Class B. It will have blank hole for properties /method of B which you cannot call as you have instance of A .

In order to avoid that, compiler stops you from doing so.

2
  • what is the solution? class B and C don't have anything in them just the name is different
    – Ben Arnao
    Feb 22, 2019 at 16:44
  • 1
    You know that, compiler doesnt. Its same in every language not just apex Feb 22, 2019 at 16:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .