-2

i have one class implemeting two different interfaces ,example:

public interface SortList{
void Sort();
search();
delete();
}

public interface unSortedList{
search();
delete();
}

class List implements ordenedList,unordenedList{}

I wanted to switch between ordened interface or not ordened list,thank you for your help.

Ulrok
  • 89
  • 2
  • 6
  • This is very unclear. Please illustrate what you want to do. – Oliver Charlesworth Feb 28 '15 at 11:31
  • You have to stick to Java syntax: can you complete the signature of the different methods like `search()` and `delete()` ? also, what does `ordenate()` is supposed to do ? – T.Gounelle Feb 28 '15 at 12:03
  • ordenate sorts the array,it's the only difference between then,i just have temperatures of days inside,i must be able to manipulate them if they are sorted or not,that's why i wanted to use two interfaces instead of one for the same class – Ulrok Feb 28 '15 at 12:20

3 Answers3

0

You have to make 2 different class like OrderedList and UnorderedList. Implements are just a guarantee you have implemented those mehtots. So you have to make a switch case or if else in your methods or you have to make two different class.

Gotrank
  • 199
  • 1
  • 7
  • so why it's usefull for a class that implements two interfaces if i must do a switch instead of decide which interface i want to implement :S – Ulrok Feb 28 '15 at 12:02
  • Because *most of the time* the two interfaces don't have methods in common OR they have the same meaning ... by design. – Stephen C Feb 28 '15 at 12:19
0

I don't know what meaning you put behind "Ordened" and "UnOrdened", and if it makes sens to have a class implementing both, but anyway, here is an example on how you can use multiple implementations of interfaces, trying to keep up with your stuff. I suppose by ordered the Java meaning as explained here, which is not sorted.

Let's say an Ordered interface gives the possibility to access some object (of type T) by index and to search or find for a given object:

public interface Ordered<T> {
    T getNth(int i);
    T find(T o);
}

Let's say an UnOrdered interface provides only the way to find an object:

public interface UnOrdered<T> {
    T find(T o);
}

Now we can defined a class that implements both interfaces:

public class MyList<T> implements Ordered<T>, UnOrdered<T> {
    List<T> theList;

    public MyList(T...a) {
        theList = Arrays.asList(a);
    }

    // The list of objects are neither sorted nor hashed. 
    // Only way to find an object is to iterate through the list
    public T find(T o) {
        for (T e : theList) {
            if (e.equals(o)) {
                return e;
            }
        }
        return null;
    }

    public T getNth(int i) {
        return theList.get(i);
    }       
}

public static void main(String[] args) {
    MyList<Integer> mylist = new MyList<>(2,8,6,1,7,3,5,9,10,4);
    System.out.println(mylist.getNth(3));
    // => print 1
    System.out.println(mylist.find(3));
    // => print 3
    System.out.println(mylist.find(42));
    // => print null
}
Community
  • 1
  • 1
T.Gounelle
  • 5,953
  • 1
  • 22
  • 32
0

You could have done a better job in describing the problem and what you are trying to solve for, assuming that I understood the problem, you can achieve this with Java 8 Default methods here is how

  public interface UrdenedList{
        void ordenate();
        default T search(){
          // default implementation here
        }
        default void delete(){
         // default implementation here
        }

    }

    public interface UnordenedList{
        default T search(){
          // default implementation here
        }
        default void delete(){
         // default implementation here
        }

    }

I wanted to switch between ordened interface or not ordened list,thank you for your help.

class List implements OrdenedList,UnordenedList{
   void ordenate(){
  // implementation here
}

T search(){
 if(condition)
    return OrdenedList.search();
  else 
    return UnordenedList.search();
}
}

void delete(){
  if(condition)
    OrdenedList.delete();
  else 
    UnordenedList.delete();
}
}

Notice Resolving conflicts by Explicitly choosing to call methods from interfaces OrdenedList, and OrdenedList

iamiddy
  • 3,015
  • 3
  • 30
  • 33