Question

If i have class Driver, and class Manager.

Manager is inherited from Driver

public class Driver { ...

.

public class Manager extends Driver { ...

I'm trying to write a simple login so the manager has a different menu to perform more actions. But I can't figure out how to store both types in an ArrayList in my Depot class.

public class Depot {

public ArrayList<Driver> arrayDrivers = new ArrayList<Driver>();

Because obviously now anything added to this array would now be a Driver, not Manager when applicable.

And when i try:

public ArrayList<? extends Driver> arrayDrivers = new ArrayList<Driver>();

I get errors with:

public Depot( ArrayList<Driver> tempDriver)     {
    arrayDrivers.addAll(tempDriver);
}

Even when i change to

ArrayList<? extends Driver> tempDriver

Can anyone help me out I cant figure this out.

I understand it would be better to create an abstract class of Users for example and then Driver and Manager but it is for coursework and i have to follow the diagram and instructions.

EDIT:

I read somewhere that wasn't the case, i must of been misinformed.

So even when i obtain the pointer like this it should still perform as Manager?

public boolean authenticateDriver(String username, String password, Driver driver) {

    Driver userDriver = GetDriver(username); //Not a manager anymore?

    if(userDriver != null) {
        driver = userDriver;
        return userDriver.checkPassword(password);
    }
    else {
        return false;
    }

}

.

public Driver GetDriver(String driver) {
    for (Driver currentDriver : arrayDrivers) {
        if (currentDriver.userName.equals(driver)) {
            return currentDriver;
        }
    }
    return null;
}

No correct solution

OTHER TIPS

I believe that:

public ArrayList<Driver> arrayDrivers = new ArrayList<Driver>();

would work. When you add managers to this ArrayList, they don't becomes Drivers. When you call .showMenu(), or something, polymorphism will guarantee that Manager's version of showMenu() is called.

Manager extends Driver, therefore if you construct an ArrayList<Driver> you can store Manager objects in it as well and they will behave like Manager, where applicable.

This is called polymorphism and it allows you to define different behaviour for various types of objects and then make use of it, without knowing, what kind of object will you receive in runtime.

Therefore, you can define different .showMenu() methods for Driver and for Manager and when you get an object user of Driver class and call user.showMenu(), an appropriate version of method will run, depending on whether user is of Manager or Driver class.

EDIT :

As to the second part of your question, basically yes, if you have the same method defined in Driver and Manager classes, an appropriate version of it will be called, depending on the class of your object. If however you have it defined only in Driver class, the same version of method will be used both for Driver and Manager objects.

If you have a method defined only in Manager, but not in Driver, you won't be able to call such method for Driver type variables, regardless of what type of object will they hold in runtime, because you can never be sure, that the variable will always contain Manager object.

Create the array list of superclass objects. Whenever you want to declare the subclass object, declare as follows:

Driver driver = new Manager(...);

Similarly, when you want to call a Driver using Manager methods, use casting.

BTW, having the name Manager include the name of the subclass (class Employee and class ManagerEmployee extends Employee) probably makes the code easier to read. I agree with you—not a fan of this system of class naming.

Generics aren't perfect. Sometimes you have to downgrade things to get through the compile and know your code maintains the contracts. If you cast the array you are passing to the addAll() call to a raw list it will work.

public Depot( ArrayList<Driver> tempDriver)     {
    arrayDrivers.addAll((List)tempDriver);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top