#Get a compile time error when storing car of wrong class in workshop object

1 messages · Page 1 of 1 (latest)

sly snow
#

I have a assignment where we are supposed to make some car-classes and a workshop that is able to store some types of cars (guess this should be specified when creating it), and which is meant to raise a compile time error when trying to add a car of the wrong class. So how can I achieve this?

jade stumpBOT
#

<@&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>.

#
TJ-Bot
I have a assignment where we are supposed to make some car-classes and a workshop that is able to store some types of cars (guess this should be specified when creating it), and which is meant to raise a compile time error when trying to add a car of the w

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 your Workshop class.
  • 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:

sly snow
#

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();
}

}

jade stumpBOT
# sly snow I have this code right now: ``` package org.example; import java.util.Set; impo...

Detected code, here are some useful tools:

Formatted code
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();
  }
}
storm goblet
#

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

sly snow
#

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>

storm goblet
#

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

sly snow
#

yes but I want to have a list of the classes to make it be random for testing

storm goblet
#

then inside ur test just create different workshops

#

new Workshop<Mercedes>(), new Workshop<Audi>(), ...

sly snow
#

I know I can do that, but isn't there any way to make it take a variable as the list of allowed types?

storm goblet
#

yes but why would u want to do that. u sacrifice compile time safety that way

#

its not meaningful

sly snow
#

okay

storm goblet
#

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

sly snow
#

can I use generics to make it be multiple classes allowed? for example workshop<Mercedes, Jaguar>?

storm goblet
#
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...
  }
}
storm goblet
#

only if u want it to always be exactly two types

sly snow
#

okay, because that is something we are supposed to add

storm goblet
#

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?

sly snow
#

''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>()

storm goblet
storm goblet
#

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"