Lets say we have a baseclass called A
and some subclasses (B
,C
,D
, etc.). Most subclasses have the method do()
but the baseclass does not.
Class AA
provides a method called getObject()
, which will create an object of type B
, or C
or D
, etc., but returns the object as type A
.
How do I cast the returned object to the concrete type and call its do()
method, if this method is available?
EDIT:
I'm not allowed to change the implementation of Class A
, the subclasses or AA
, since im using a closed Source API.. And yeah, it does have some design issues, as you can see.
You can test with
instanceof
and call thedo()
methods:First of all it would be a better approach to make
Class A
as an abstract Class withdo()
as an Abstract method in it......Moreover if you still want the way you want to do it..then
Do an explicit cast.
B b = (B) a; // a is a casted back to its concrete type.
Moreover you should keep in mind this very important behaviour of the Compiler.
The Object Reference Variable of Super Type must have the method to be called, whether the Sub Type Object has or not.
Eg:
- To call a method,
do()
onObject Reference Variable
of Type A, classA
must have thego()
method.Assuming
A
definesdo
, and it is not private, you can just call it without a cast, no matter the subclass thatAA
returns. That's one of the features of polymorphism. At runtime, the interpreter will use the correct (i.e. the implementation of the actual class) version ofdo
.What you are describing seems to me like you want to invoke
Derived Class
methods onBase class
reference..But for that, you need to have your methods in your
base class
also..So, you need to declare your method
do()
in your base classA
also.. If you don't want to give an implementation, let it be abstract, or let it be an empty method.. It will not matter..Now, if you do the same thing you're explaining.. You won't need to do a typecast..
Because, appropriate
Derived Class
method will be invoked based upon -which derived class object does your base class reference point to
Ok, just now saw your edit, that you are not allowed to change your classes..
In that case, you will actually need to do a
typecast
based on the instance of returned reference..So, in
main
method above, afterA obj = returnA();
this line add the following line: -But, in this case, you would need to check
instanceof
on each of your subclasses.. That can be a major problem..You can do this with a little work if the method invocations return instances of the class in question, which is your specific question (above).
Test class A
Test class B
Test Class C
The Chain class
Chain's helper class.
Output:
I think a better idea is to actually have class
A
define thedo()
method either as an abstract method or as a concrete empty method. This way you won't have to do any cast.If you are not allowed to change any of the classes than you could define a class
MyA extends A
which defines thedo()
method andMyB
,MyC
,... and aMyAA
that would basically do whatAA
does, just that it returns objects of typeMyB
,MyC
....If this is not ok then I don't see another way than checking if the returned object is of type
B
and do a cast toB
and so on.