I'm wondering if I'm creating/breaking down object types into meaningless classes and if there is a better way to do it.
Say I've got the following classes (--> extends the higher class):
Vehicle class - takes care of movement and a default speed
-->Car class - adds passengers
-->-->ElectricCar class - a default constructor
-->-->GasolineCar class - a default constructor
-->-->HybridCar class - a default constructor
-->Motorcycle class - a default constructor
-->Bike class - a default constructor, overrides speed
-->Segway class - a default constructor, overrides speed
As you can see, they're mostly default constructors. So I'm only creating them in case I ever need to use an instanceof conditional statement. Most of my code ends up in the Vehicle class because I'm trying to avoid repeating the same code in every class. Also, I should make Vehicle abstract, correct? I never create a Vehicle object.
Is this OK or is there a better way to do it?
Therefore, only two instances of class A are created. Save this answer.
As the name suggests, instanceof in Java is used to check if the specified object is an instance of a class, subclass, or interface. It is also referred to as the comparison operator because of its feature of comparing the type with the instance.
The isInstance method is equivalent to instanceof operator. The method is used in case of objects are created at runtime using reflection. General practice says if the type is to be checked at runtime then use the isInstance method otherwise instanceof operator can be used.
instanceof is a binary operator we use to test if an object is of a given type. The result of the operation is either true or false. It's also known as a type comparison operator because it compares the instance with the type. Before casting an unknown object, the instanceof check should always be used.
First, as long as the code functions and solves the task at hand I would say it is "correct" enough (so yes, it is okay). That being said, some people prefer to use interface(s) instead of abstract classes (because you can implement multiple interfaces but only directly extend from one ancestor). Further, if you're using Java 8+ you can add default
methods to interface(s). Other alternatives might include having one Vehicle
class but a VehicleType
enum
field for the Vehicle
class.
There's never such a thing as "too many classes" as long as your classes make sense and serve a function. There is however such a thing as pointless abstraction
in my opinion.
If you're 100% sure that you will only ever use these classes to determine the vehicle type (quote from your question),
I ever need to use an instanceof conditional statement
Then maybe having sub classes qualifies as pointless abstraction. Just use an enum instead which describes your vehicles type and move on.
If however you anticipate or plan on using your sub classes for more than just the type, it's completely fine to do it by having sub classes and it's in fact preferred to having lots of if/else or switch statements in your Vehicle class.
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