Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Interesting problem. I am trying to creating a databanks class which will contain multiple instances of various databank classes which extend a databank interface. Each databank will contain a list or multiple lists each containing a different type.

So.
Java
databank.getXdatabank.getData.add(y);


The problem...
As I understand it all of the methods of an interface must be implemented. I was hoping not to need to specify the type that each list contains but rather that it just returns a list.

Java
public interface Idatabank {
    List<what do I put here> getData();
}

public Xdatabank implements Idatabank {
    List<int> whatever1;

    public List<int> getData() {
        return this.whatever1;
    }

}

 public Zdatabank implements Idatabank {
    List<String> whatever2;

    public List<String> getData() {
        return this.whatever2;
    }
}


What I have tried:

I tried putting
Java
List<?>
in the interface.

Then with
Java
container.getDataBanks().getXdatabank().getData().add(y);
the method is not applicable for the arguments..
Posted
Updated 3-Jun-16 22:12pm
v6

You need to parametrize your interface definition:
Java
import java.util.List;

public interface Idatabank<e> {
	List<e> getData();
}

Then define the implementation like this:
Java
public class Xdatabank implements Idatabank<integer> {
	List<integer> whatever1;
	public List<integer> getData()
	{
		return whatever1;
	}
}
 
Share this answer
 
v2
Comments
Michael Hurt 4-Jun-16 5:28am    
Thanks. Looks interesting, I will try this and accept if works.
Michael Hurt 4-Jun-16 5:41am    
What about the other way for setting data? I get an error with

boolean setData(List<e> data);

@Override
public boolean setData(List<string> data) {}
Michael Hurt 4-Jun-16 6:19am    
Ok, I get it now. Worked perfectly.
Thanks.... :)
Sergey Alexandrovich Kryukov 4-Jun-16 11:07am    
Correct, a 5.
—SA
List<object> will do if there is not a better way.
 
Share this answer
 
Comments
markkuk 4-Jun-16 4:10am    
Won't work, see: https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html for the reason.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900