As you know the 'program to an interface' design principle broadly prefers supertypes instead of concrete types or implementations.
Is it consistent with the principle to use instanceof in a Java program to derive a concrete type from a supertype?
In my application, Storehouse is an abstract supertype class with a couple of private variables and public getters and setters.
ConcreteStorehouseA inherits from Storehouse and has a lot of concrete methods and variables. ConcreteStorehouseB is similar but different.
My application receives a Storehouse. However, Storehouse is not a useful type to operate on. Because the only really useful methods are contained in the concrete types, I use instanceof as follows:
if (storehouse instanceof ConcreteStorehouseA) {
ConcreteStorehouseA concreteStorehouseA = (ConcreteStorehouseA) storehouse;
// perform operations on the concrete type's useful methods and variables
Is using instanceof compatible with the principle?
Edit:
In essence the application is a dice simulator for a table top RPG, Shadowrun. The concrete types are the different test types - Success Test, Opposed Test, Extended Test - which all have very different factors and parameters for their successful operation. The supertype essentially contains the dice pool!
instanceof can be used to test if an object is a direct or descended instance of a given class. instanceof can also be used with interfaces even though interfaces can't be instantiated like classes.
Is a class an Instanceof an interface? If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.
"Programming to an interface" means, that when possible, one should refer to a more abstract level of a class (an interface, abstract class, or sometimes a superclass of some sort), instead of refering to a concrete implementation.
By programming to an interface, you are more likely to apply the low coupling / high cohesion principle.
As a rule of thumb, that "program to interfaces" principle that you mentioned can be translated into: import
only the interface type, don't have any compile-time dependency on subclasses.
Therefore, the answer to your question would be definitely no. You are not programming to interfaces since you cast to concrete types.
You've said it yourself:
My application receives a Storehouse. However, Storehouse is not a useful type to operate on. Because the only really useful methods are contained in the concrete types
In other words, your Storehouse
abstraction isn't buying you anything... why do you have it?
Could you create abstract methods in Storehouse
, implemented in each concrete class, which would then let you treat the concrete types the same way in your client code? That's the goal of the abstraction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With