40

I have an abstract class Vehicle with 2 implemented subclasses RedVehicle and YellowVehicle.

In another class I have a List<Vehicle> containing instances of both subclasses. I want to be able to pass into a method a class type and then use that type to decide which set of objects I want to do something to in the List.

Since Class is generic I should parameterise it with something, however putting the parameter as the parent class Vehicle stops the calling code working since exampleMethod is now expecting a type of Vehicle, not a subclass of RedVehicle or YellowVehicle.

I feel there should be a clean way to do this so what would be the correct way to implement the functionality?

n.b. I don't necessarily have to pass in the Class type, if there are better suggestions I'd be happy to try those.

Calling code:

service.exampleMethod(RedVehicle.class);
service.exampleMethod(YellowVehicle.class);

Fields/Method:

//List of vehicles
//Vehicle has 2 subclasses, RedVehicle and YellowVehicle
private List<Vehicle> vehicles;

//Having <Vehicle> as the Class parameter stops the calling code working
public void exampleMethod(Class<Vehicle> type) 
{
    for(Vehicle v : vehicles)
    {
        if(v.getClass().equals(type))
        {
            //do something
        }
    }
}
Nissa
  • 4,636
  • 8
  • 29
  • 37
Peanut
  • 2,221
  • 1
  • 26
  • 37

4 Answers4

76

Do this instead:

public <T extends Vehicle> void exampleMethod(Class<T> type) 
mprivat
  • 21,582
  • 4
  • 54
  • 64
  • I knew it would be something simple. Is there a special name for having the generic type before the return type/a link to a general source would be nice. I'll try and tomorrow and accept if it works thanks. – Peanut Oct 24 '12 at 21:19
  • 1
    have a look at oracle's generics introduction or angelika langer's java generics FAQ. +1 for mprivat's direct solution. – DaveFar Oct 24 '12 at 21:57
  • 1
    @DaveBall Thanks for the angelika langer suggesetion, I'm aware of generics as a concept and have used them but have never looked at them in depth, this looks like a great in-depth discussion of them. The link for anyone else interested: http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html – Peanut Oct 25 '12 at 00:49
  • A little elaboration of this answer can be found [here](http://stackoverflow.com/a/9421315/1276636) – Sufian Oct 18 '14 at 13:14
  • @Peanut They are called generic methods – veritas Sep 24 '20 at 18:37
  • I understand the Class part in the argument to support type Vehicle. What I now understand is that if a method uses a generic in its argument, it needs to be added to the method syntax structure before the return type.of the method too – veritas Sep 24 '20 at 18:53
3

Why don't you use the visitor pattern?

That way you

  • don't need type tokens
  • let dynamic dispatch handle the case distinction (instead of if(v.getClass().equals(type)))
  • are more flexible (following OCP)

In detail:

your abstract class Vehicle gets a method accept(Visitor v), with the subclasses implementing it by calling the appropriate method on v.

public interface Visitor {
  visitRedVehicle(RedVehicle red);
  visitYellowVehicle(YellowVehicle yellow);
}

Using a visitor:

public class Example {

  public void useYellowOnly() {
    exampleMethod(new Visitor() {
        visitRedVehicle(RedVehicle red) {};
        visitYellowVehicle(YellowVehicle yellow) {
             //...action
        });
  }
  public void exampleMethod(Visitor visitor){
      for(Vehicle v : vehicles) {
          v.accept(visitor);
      }  
  }
}
DaveFar
  • 7,078
  • 4
  • 50
  • 90
1

I have found this syntax to be working as expected:

public void exampleMethod(Class<? extends Vehicle> type) 
not2savvy
  • 2,902
  • 3
  • 22
  • 37
0

The accepted answer works and got me where I wanted to go. I thought I would add this just to make it clearer to anyone who might need it.

In this case RevisedExposure is a sub-class of Exposure. I need to call GetMetadata() with a list of either of these, which results in the same result set.

private async Task<List<Metadata>> GetMetadata<T>(List<T> exposures) where T : Exposure

Now I can call this method from two places with different versions of the list like this.

var metadata = await GetExposureMetadata(revisions);

or

var metadata = await GetExposureMetadata(exposures);

works great!

Don Rolling
  • 2,301
  • 4
  • 30
  • 27