0

I am working on a school project that involves a parking lot. Our 'parking lot' contains a 10x10 grid and each space can either contain a vehicle, or be empty.

I have a superclass of vehicles and multiple subclasses of the vehicle class, such as motorbikes and vans that inherit attributes and methods from the superclass.

I have a method which returns a linked list of all the neighbours of each parking space. However the linked list stores the objects as the superclass type vehicle, no matter what subclass the object actually is.

I want to add methods that are dependent on what subclass of vehicles is a neighbour of a given spot ie whether it contains a motorbike or not.

How would I go about implementing this in my project? Thanks in advance.

I was expecting to be able to get the subclass type of each vehicle in the linked list but could not.

4
  • wdym with add methods that are dependent on what subclass of vehicles is a neighbour ? Commented Feb 18, 2024 at 20:36
  • the project i am doing is similar to a cellular automaton. there are rules such that if there are too many of a certain type of vehicle in an area around a spot, other vehicles cannot park there, hence i need to be able to get the subclass type of the vehicle Commented Feb 18, 2024 at 20:39
  • you can get the subclass using v.getClass() and check on the name OR create a property (maybe a enum) to perform this operation Commented Feb 18, 2024 at 20:43
  • you can also test if some object is an instance of a given Class by using instanceof (e.g. if (obj instanceof MotorBike) { ... }) but the more Object Oriented solution is using methods like having each Vehicle having a getType() or isMotorBike() method Commented Feb 18, 2024 at 21:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.