foges
foges

Reputation: 1536

Java generics problem

I'm working on a project where I am trying to use generics but I'm having a hard time.

Say I have a many cars, all of them implemented in a different class (eg. audi.java, toyota.java, etc..) and each of them have a method called startCar(). Now I want to create a class Cars that lets other classes use these cars. However the following will not work. How should I go about implementing it?

public class Cars {    
 private MyCar mycar;
 private class MyCar <MySpecificBrandOfCar>{
    private MySpeicifBrand mySpecifcCar;
    public MyCar(){}
 }
 public Cars(){
     myCar = new myCar<audi>;
 }
 ..other methods..
}

I also tried creating an interface, but that gave errors too.

Thanks

EDIT, attempted nfechner's suggestion

Thanks for all the responses!

Would the following work (NB:GenericCar is an interface that is implemented by all cars (eg. audi.java) and has functions such as startCar()

public class Car{
    private GenericCar mycar;
    public Car(){
        mycar = new audi();
    }
}

Upvotes: 2

Views: 265

Answers (5)

DaveFar
DaveFar

Reputation: 7467

You should not use generics here. (Think of them as type-parameters you do not want to fix, e.g. as for Collection).

If cars of different brands do behave differently, do inheritance instead, like Jonathan suggests.

But a better design is using composition instead of inheritance, i.e. Cars have a field of the class Brand. This is especially the case if the behavior is not strongly influenced. And I don't think an Audi behaves that much different from a Toyota.

Furthermore, I don't think there are that many brands, and you probably only need one instance of each brand. So use an enum instead of a class for Brand. In detail:

public class Car { 
 public static enum Brand { Audi, Toyota };    
 private Brand brand;

 public Cars(Brand brandParam){
     this.brand = brandParam;
 }
 //..other methods, getter and setter for brand
}

You can put methods in the Brand enum for the small and specific varying behavior. I think Josh Bloch, the author of Java Enums, explains their capabilities very well in Effective Java, 2nd edition.

Finally, try to choose better names for your classes and variables (I don't think MyCar is telling you anything).

Upvotes: 2

mohdajami
mohdajami

Reputation: 9690

In this example Inheritance and Polymorphism will be a better solution than Generics.

Check this example. Java Tutorial - Inheritance and Polymorphism

Upvotes: 3

Keith Irwin
Keith Irwin

Reputation: 5668

This is not a circumstance where using generics is appropriate. You should use an interface or a superclass. I'm not sure what errors you were seeing with an interface, but it would be easier to fix them than to try to fix your problems with this approach.

Upvotes: 1

Jonathan Weatherhead
Jonathan Weatherhead

Reputation: 1580

Generics can't be used to instantiate a class unfortunately, not in Java. Perhaps you could be a bit more specific about your intention, but you might find it useful to have some base class Car and have each specific car extend it. If you define the startCar() method in the base class Car, every subtype of Car will also have this method through polymorphism.

public class Car{
    public void startCar(){ }
}

public class Audi extends Car{}

public class Toyota extends Car{}

and so on.

Upvotes: 5

nfechner
nfechner

Reputation: 17545

Your code looks a bit overengineered. What's wrong with a simple Car interface, that all car types implement?

Upvotes: 3

Related Questions