i once had the same question. My teacher answered it by saying that interfaces are a way of organizing similar classes. So, technically they serve no functionality to a class. Although it can be useful. Say we have a class that calls for a Comparable
public Example(Comparable c)
{
//code
}
As you can see, sometimes constructors or methods may call for an object that implements a certain interface. So if we wanted to use this example class with another class, we must make the other class implement Comparable, and of course defining compareTo(). This is a common thing because sometimes classes need to make sure that the objects can be compared. I'll give you a perfect example. The class TreeSet.
"but a TreeSet instance performs all key comparisons using its
compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the set, equal."
This shows how TreeSets must be used on objects that can be compared because TreeSets sort the elements according to their compareTo definition. So, let's say that we have a class called BankAccount, that implements Comparable, so we must define compareTo().
public class BankAccountA implements Comparable
{
private int balance;
public BankAccountA(int initialBalance)
{
balance = initialBalance;
}
public int getBalance()
{
return balance;
}
public int compareTo(Object bank)
{
BankAccountA b = (BankAccountA)bank; //We must cast the parameter as a BankAccountA
if(balance == b.getBalance())
return 0; //They are equal
else if(balance < b.getBalance())
return -1; //The balance is less than the other balance
else
return 1; //The balance is greater than the other balance
}
public String toString()
{
return "Balance = " + balance;
}
}
In this case, the accounts are compared according to their balance. Now, let's make another BankAccount class that compares accounts by account number.
public class BankAccountB implements Comparable
{
private int account;
public BankAccountB(int accountNumber)
{
account = accountNumber;
}
public int getAccountNumber()
{
return account;
}
public int compareTo(Object bank)
{
BankAccountB b = (BankAccountB) bank; //We must cast the parameter as a BankAccountB
if(account == b.getAccountNumber())
return 0; //They are equal
else if(account < b.getAccountNumber())
return -1; //The number is less than the other numer
else
return 1; //The number is greater than the other number
}
public String toString()
{
return "AccountNumber = " + account;
}
}
So now we will make a tester class to demonstrate how this will work. I'm going to use TreeSet. Ignore the iterator, just know that it allows you to retrieve the objects in the set.
import java.util.*;
public class BankAccountTest {
public static void main(String[] args)
{
Set<BankAccountA> aSet = new TreeSet<BankAccountA>();
aSet.add(new BankAccountA(50));
aSet.add(new BankAccountA(25));
aSet.add(new BankAccountA(100));
Iterator<BankAccountA> iter = aSet.iterator();
System.out.println("BankAccountA Info: ");
while(iter.hasNext())
{
//Since System.out.println() calls toString() on its parameters
//which we defined in the class, we can have put the object in println(),
//if we didn't define toString(), this statement would print the memory location
//of the object because that is the default purpose of toString()
System.out.println(iter.next());
}
/*
* This shows the way it works for BankAccountB
*/
Set<BankAccountB> bSet = new TreeSet<BankAccountB>();
bSet.add(new BankAccountB(2000));
bSet.add(new BankAccountB(1000));
bSet.add(new BankAccountB(4000));
Iterator<BankAccountB> iter2 = bSet.iterator();
System.out.println("-------------------");
System.out.println("BankAccountB Info: ");
while(iter2.hasNext())
{
System.out.println(iter2.next());
}
}
}
As you will see, the program outputs:
Balance = 25
Balance = 50
Balance = 100
-------------------
BankAccountB Info:
AccountNumber = 1000
AccountNumber = 2000
AccountNumber = 4000
Now, if we were to make it so that BankAccountA and B do NOT implement Comparable, here is the error we would get
in the tester:
Exception in thread "main" java.lang.ClassCastException: BankAccountA cannot be cast to java.lang.Comparable
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)
at BankAccountTest.main(BankAccountTest.java:8)
This indicates the use of interfaces: some classes can call for objects that implement a certain interface, in the case of TreeSet,
the object must be a Comparable. I know this is extremely long, but I think it will help you understand. Hope this helps man!