#Get a compile time error when storing car of wrong class in workshop object
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Your compile-time error likely comes from a type-safety issue with generics in your Workshop class. If your workshop is designed to store only specific car subclasses, it should be declared with a generic type parameter such as Workshop<T extends Car>. Then when you create an instance, specify the exact car class it can accept, e.g., Workshop<Sedan> workshop = new Workshop<>();. This ensures the compiler enforces that only Sedan or subclasses thereof can be stored.
If you try to add a different car type, the compiler will raise an error as expected. This is intentional and a benefit of generics because it catches type mismatches at compile time rather than runtime.
So, the key points are:
- Use a generic type parameter with an upper bound (
<T extends Car>) in yourWorkshopclass. - Specify the exact subclass when creating a workshop instance.
- The compile-time error is the expected safety feature of Java generics, not a bug.
If your current code doesn’t use generics or uses raw types, that could be why you see errors.
Useful links:
Is there any way to have a parameter when creating a instance of the workshop that describes what types it is allowed to store?
I have this code right now: ```
package org.example;
import java.util.Set;
import java.util.ArrayList;
public class Workshop {
private int maxCars;
private Set<?> allowedTypes;
private ArrayList<Car> storedCars = new ArrayList<>();
public Workshop(Set<?> types, int max) {
allowedTypes = types;
maxCars = max;
}
public <T extends Car> void storeCar(T car){
if (storedCars.size() == maxCars) {return;}
storedCars.add(car);
}
public Car retrieveCar() {
return storedCars.removeLast();
}
}
Detected code, here are some useful tools:
package org.example;
import java.util.Set;
import java.util.ArrayList;
public class Workshop {
private int maxCars;
private Set<? > allowedTypes;
private ArrayList<Car> storedCars = new ArrayList<>();
public Workshop(Set<? > types, int max) {
allowedTypes = types;
maxCars = max;
}
public <T extends Car> void storeCar(T car) {
if (storedCars.size() == maxCars) {
return ;
}
storedCars.add(car);
}
public Car retrieveCar() {
return storedCars.removeLast();
}
}
yes, what u want is called generics
drop that wildcard <?> and implement proper generics
will look sth like this:
public class Workshop<C extends Car> {
...
private List<C> cars = new ArrayList<>();
...
public void storeCar(C car) {
...
cars.add(car);
}
public C retrieveCar() { ... }
}
if ur confused by this, read a tutorial about generics first 🙂
(or ask us concrete questions to it 👍)
think about the class Workshop<C extends Car> like a parameter in a method. just that its type-parameter for the class
people who create instances of the class can then choose the type they want
new Workshop<Mercedes>()
and so on
its the same mechanism that arraylist uses as well
now how can I get a list of classes? I tried this Class<? extends Vehicle> vehicles = new Class[] {Mercedes.class, Ford.class} but it didn't work, got the error that I can't convert Class[] to Class<? extends Vehicle>
ur overcomplicating it
the user specifies the type on creation of the workshop
new Workshop<Mercedes>()
and that Mercedes is then ur C
so u dont have to do anything anymore inside the class
just what i wrote above
yes but I want to have a list of the classes to make it be random for testing
then inside ur test just create different workshops
new Workshop<Mercedes>(), new Workshop<Audi>(), ...
I know I can do that, but isn't there any way to make it take a variable as the list of allowed types?
yes but why would u want to do that. u sacrifice compile time safety that way
its not meaningful
okay
if u want to have workshops that sell only mercedes and audi but not fiat, then u wont be able to get that with generics
and not with compile-time safety
in that case u would indeed take a list of class tokens
can I use generics to make it be multiple classes allowed? for example workshop<Mercedes, Jaguar>?
class Workshop {
private List<Car> cars = new ArrayList<>();
private Set<Class<? extends Car>> tokens;
public Workshop(List<Class<? extends Car>> tokens) {
this.tokens = new HashSet<>(tokens);
}
void addCar(Car car) {
tokens.findAny(t -> t.isAssignableFrom(car.getClass()))...
}
Car getCar() {
cars...
}
}
no
only if u want it to always be exactly two types
okay, because that is something we are supposed to add
i have some doubts that u interpreted the task correctly
bc this is just super unusual and not the right way to approach it
could u share the assignment?
''some workshops are supposed to store a specific type of cars; others can store any type of cars''
does that mean that either I have one specific type and do Workshop<Type> workshop = new Workshop<Type>() or for any type I do Workshop<Car> workshop = new Workshop<Car>()
yeah. so u have that simply by generics already
exactly
new Workshop<Mercedes()
and new Workshop<Car>()
one is mercedes only, the other allows all of them
but strict-mixtures arent really doable
and i dont think they ask for this
as in "make a workshop that can only deal with mercedes and audi but not fiat"