Lecture 42, CS 302-7 and 8, May 4

  1. Various things
    1. Program 4

                                                               i.     Partner deadline – tonight

                                                             ii.     Extra credit posted

  1. Review:
    1. The Object Class

                                                               i.     Every Class extends Object

                                                             ii.     Override methods

1.    equals(Object o), toString()

                                                            iii.     instanceof

    1. Interfaces

                                                               i.     Definition – a contract between interface and implementing classes

                                                             ii.     Defining

1.    Syntax

2.    Interface type

3.    Abstract methods

4.    static final constants

  1. Defining an interface
    1. Syntax:

public interface <name>{

            <returnType> <methodName>(<params>);

…

}

    1. A couple of notes:

                                                               i.     Do not put any code in methods

                                                             ii.     Do not say methods are public – they always are

                                                            iii.     No instance variables

                                                           iv.     No static methods

                                                             v.     Pay attention to the semicolon after the method declaration(s).

    1. You can also include constants in your interface declarations – Syntax:

<type> <CONSTANT_NAME>=<value>;

                                                               i.     Note – these are always public static final, even though we don’t explicitly say so

  1. Using interfaces
    1. Indicate that the class you are creating implements an interface

public class <name> implements <interface1>,<interface2>…

    1. Define all methods

                                                               i.     Stubs are ok though

    1. Then, in your code, you can treat the interface type kind of like a superclass:

<InterfaceName> o=new <ClassThatImplementsInterface>();

                                                               i.     Thus, you know that object must implement the behavior defined in the interface

  1. Example – Shape, square, circle, shapeTester
  2. Comparable
    1. Comparable is an interface included in the Java standard package
    2. All classes that implement comparable must implement a.compareTo(Object b) method

                                                               i.     http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html

    1. Once you define an interface, there are certain things that you should not change

                                                               i.     Don’t add or change methods

    1. compareTo – returns a negative number if a comes before b, zero if a and b are the same, positive number otherwise

                                                               i.     Note: not –1,0,1.  Can be any negative or positive values

  1. Homework – For Monday – work on Program 4