简体   繁体   中英

how to make method parameter type ArrayList<Object> take different object types

I have an abstract method as part of an abstract class with the following declaration:

abstract public ArrayList<Device> returnDevices(ArrayList<Object> scanResult);

I want the parameter that is passed to be an ArrayList, but the type object in the ArrayList will be dependent on the child class that inherits this superclass and implements the method returnDevices.

I thought that I could achieve this by making the method abstract as above, and then in the child class that inherits it do something like:

public ArrayList<Device> returnDevices(ArrayList<Object> scanResult) {

    Iterator<Object> results = scanResult.iterator();
    while(results.hasNext())
        Packet pkt = (Packet) results.next();  // HERE: I cast the Object
}

That is fine and does not cause an error, but when I try to call returnDevices by using a parameter of type ArrayList<Packet> , like the following:

ArrayList<Packet> packets = new ArrayList<Packet>();
// <----- the "packets" ArrayList is filled here
ArrayList<Device> devices = returnDevices(packets);

... I get the error:

The method returnDevices(ArrayList<Object>) in the type ScanResultParser is not applicable for the arguments (ArrayList<Packet>)

So clearly it is rejecting the parameter type. What is the proper way to achieve what I am trying to do?

- This is how Collections are made type-safed in Java so a wrong type doesn't enter into the Collection , As collection are checked only during the `Compilation time and Not during the Runtime .. ie a Cat object should not enter into a Collection of type Dog.

You can do it this way...

public ArrayList<Device> returnDevices(ArrayList<? extends Object> scanResult)

Or

public <T extends Object> ArrayList<Device> returnDevices(ArrayList<T> scanResult)

The problem is, even though Packet is-a Object , ArrayList<Packet> is not a ArrayList<Object> . Generic type is invariant in Java .

You can use the extends and super keywords with your type parameters to introduce that type of effect in Java generics.

I think the following is what it should be

abstract public <T extends Device> List<T> returnDevices(List<T> scanResult);

With that the method with either take a list of Device s and return alist of Device s or take a list of some subtype of Device and return a list of the same subtype.


Side note: Coding against some concrete type (eg ArrayList ) is not a good practice. You should always code against the interface whenever possible (eg List ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM