Title: interfaces
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Interfaces

1. The basic intent of an interface is to specify some methods that a class provides. Interfaces allow Java to avoid going down the route of trying to have a class derived from more than one base class (C++ chose this). This route has problems. Java makes heavy usage of interfaces. The data structures are all based on interfaces. JDBC provides a better database architecture than ODBC.
See Ordered.java in Book files
2. Some of the interfaces we have encountered so far include: Runnable, MouseListener, MouseMotionListener, KeyListener, ActionListener.
3. You can create an abstract class that implements some of an Interface and declares a prototype for any unimplemented methods.
See MyAbstractClass.java in Book files
4. You can create an extended interface from a given interface. By extending the JDBC interfaces, companies like Oracle can comply with JDBC and still provide their own enhancements on top of JDBC.
See the ShowablyOrder.java in Book files.
5. Java has many critical interfaces that we will encounter: Comparable, Cloneable, Runnable, Serializable, etc. The Comparable interface can be used to create a sort routine that can be used by many different classes. Compare the following sort routines:
SelectionSort.java in chapter 6
GeneralizedSelectionSort.java, and ComparableDemo.java in chapter 13
6. The Comparable interface can be implemented in any reasonable fashion, but the following properties should be present:
  - For no object x does x come before x
  - For any 2 objects x1 and x2, one and only one of the following holds true: x1 comes before x2, x2 comes before x1, or x1 equals x2
  - if x1 comes before x2 and x2 comes before x3, then x1 comes before x3.
7. Interfaces can be used to provide define constants. Note that any variable definition needs to be static and final. So ... even if you declare a constant as not static or final, Java will make it static and final. By the way, this usage of interfaces seems to be contrary to the normal interface intent.


	public interface MonthNumbers
	{
	  public static final int JANUARY=1, FEBRUARY=2, MARCH=3, APRIL=4,
	            MAY=5, JUNE=6, JULY=7, AUGUST=8, SEPTEMBER=9,
				OCTOBER=10, NOVEMBER=11, DECEMBER=12;
	}
	
	public class DemoMonthNumbers implements MonthNumbers
	{
	    public static void main(String[] args)
		{
		   System.out.println("The number for Januaray is "+ JANUARY);
	}
	
	
	// Note that the following interface is equivalent to the previous one, because
	// any variables in an interface must be static final anyway even if you don't declare
	// them as such
	
	public interface MonthNumbers
	{
	  public  int JANUARY=1, FEBRUARY=2, MARCH=3, APRIL=4,
	            MAY=5, JUNE=6, JULY=7, AUGUST=8, SEPTEMBER=9,
				OCTOBER=10, NOVEMBER=11, DECEMBER=12;
	}
	

8. The Serializable has no methods or constants. We have discussed Serializable and discovered that it acts more like a tag on a class that says its OK for Java to generate serialized output on demand.
9. Object.clone does a bit-by-bit copy of an objects data to create another class. However, if the object cloned does not implement the Cloneable interface, then Object.clone throws the CloneNotSupportedException which is compiler checked. So if you want to call this method, you must implement Cloneable. The Cloneable interface has no methods. When you define a class to implement the Cloneable interface, you are agreeing to redefine the clone method (inherited in the Object class) in a particular way.This is fine if the data in the object consists of primitives and immutable classes like (String, Integer, Float, etc.). However, problems can occur when the object contains references to mutable classes or arrays. See the book examples YourCloneableClass.java and YourCloneableClass2.java.
Consider the following things to try regarding Cloneable
10. Create a class which contains only primitives and mutable classes like String. Use the clone method in the Object class to create another instance of the class. Print the contents of both classes. Change the contents of one of the variables in the cloned copy and again print out the contents of both classes. Note you will need catch the CloneNotSupportedException.
11. Repeat the above experiment, but this time try to add a mutable class to the class that you clone. The mutable class can be a trivial Data class with a single int in it. You should now get an exception when you call clone. To fix it, just add the promise that you have implemented Cloneable. This is a promise (we are going to lie the first time out), that we have an appropriate clone routine. Repeat the experiment of modifying the mutable variable and print out both classes.
12. This chapter also discusses Inner classes. We have encountered them in our GUI work. See the BankAccount.java and BankAccountDemo.java examples from the book (chapter 13).

Self-Test Exercises:

1. Can you have a variable defined in an interface type?
2. Can an abstract class every implement an interface?
3. Can a derived class have 2 base classes? Can it implement two interfaces?
4. Can an interface implement another interface.
5. The method interchange in GeneralizedSelectionSort makes no use of the fact that its second argument is an array with base type Comparable. Suppose we change the parameter type Comparable[] to Object[] and change the type of the variable temp to Object. Would the program produce the same dialogue?
6. is the following a suitable implementation of the Comparable interface?


  public class Double2 implements Comparable
  {
     private double value;
	 public Double2(double theValue)
	 {
	    value = theValue;
	 }
	 public int compareTo(Object other)
	 {
	     return -1;
	 }
	 public double doubleValue()
	 {
	    return value;
	 }
 }


Last Updated: November 20, 2013 4:15 PM