java-interfaces

Java Interfaces Question

Hi I have the following interfaces that I need to create. Can you just answer one question. Am I supposed to create a variable to hold the value of the operator and the values of the two operands? If so, should these be created inside the interfaces or inside the class with my main method? interface ArithmeticOperator { // Returns...

Using generics to create max function that returns the larger one

In Java, how would I use generics to create a max function that takes as parameters two Comparable objects of the same type and returns the larger one? I tried: public static <T extends Comparable> T max(T obj1, T obj2) { return ( ((obj1).compareTo(obj2) >= 0) ? obj1 : obj2); } (It returns obj1 if they are both equal.) The metho...

Java - declaring from Interface type instead of Class

In my quest to correctly grasp Interface best practices, I have noticed declarations such as: List<String> myList = new ArrayList<String>(); instead of ArrayList<String> myList = new ArrayList<String>(); -To my understanding the reason is because it allows flexibility in case one day you do not want to implement an ArrayList but m...

Java: Put objects of different types into a ArrayList for a ModelCollection. Interaces?

I want to have my models wrapped up in a ModelCollection which is mostly used for a ListView. The Collection has always the same attributes like title, totalResults (for Pagination) and it shall contain the listItem-Models in the ArrayList "items". However, these models have different types like "ModelCategory" or "ModelChain" and often...