saferJo
saferJo

Reputation: 547

Abstract class logic problems

I am trying to make one program and here is my problem:

I have interface 'Engine'.

EngineFactory.createEngine(request)

In the factory I have:

if(request instanceof TypeA){ return new TypeAAA(request);}
if(request instanceof TypeB){ return new TypeBBB(request);}

Sadly in the constructors when I try to do:

(TypeA) requestArg

It is saying - undefined constructor. Can I do that without the obvious casting of (TypeAAA)TypeA requestArg)?

Upvotes: 0

Views: 139

Answers (1)

Casper
Casper

Reputation: 511

I assume you want to cast it to TypeA and not TypeAAA if it's instanceof TypeA?

Anyway, yes, you still have to cast it yourself even though you checked if it was an instanceof the class just prior. In Kotlin the compiler would be smart enough to figure it out but with Java you still have to do the cast.

Upvotes: 1

Related Questions