Week #14 Discussion Details

Discussion 13 (Thanksgiving Holiday)

Refer to the Discussion 13 notes for a more complete context regarding these notes and sample exercises.

Interfaces

Jave uses the interface to define a weak inheritance relationship between a subclass and a competely abstract type representation. Note, this is not an abstract class. The interface will include only constant properties and abstract methods. It will not include any instance properties and will not include any implemented methods. We have examined and used different interfaces this semester, including: Comparable, Iterator, and List.

Casting

Whenever a value is represented in a certain type and then assigned to a different type, the assignment is permitted provided it follows the rules of casting.

Keyword

Recall we have discussed the keyword:
implements

The keyword implements is used to create the weak inheritance relationship between a subclass and an interface. First the class being created is listed as usual (subclass), then implements, followed by the interface being implement. As always, a single class may be extended, the extension being listed before the declaration of the implemented interface. Also, more than one interface may be implemented. Each implemented interface is placed in a comma separated list.
Here is the generalized format for implementing a single interface.
class Subclass1 implements Interface1 {
  // SubClass implementation
}
Here is the generalized format for extending a class and implementing an interface.
class Subclass2 extends Superclass2 implements Interface2 {
  // SubClass implementation
}
Here is the generalized format for implementing multiple interfaces.
class Subclass3 implements Interface1, Interface2, ..., InterfaceN {
  // SubClass implementation
}

Exercises

Please note, it is likely you will not have the time to complete all of these exercises during your discussion. They are given to you so that you can review and practice using our recent topics. Use them as a study aid during the rest of the week to supplement the home work given in the project assignments.
  1. Modify your solution to the analysis of the class Car so that it will implement the interface Comparable.
  2. Modify your solution to the analysis of the class Truck so that it will implement the interface Comparable.
  3. Create the interface Light. Light will be implemented by the classes TrafficLight and RailroadCrossingLight. Light will contain the method change. A TrafficLight will change from red to green to yellow to red. A RailroadCrossingLight will change from flashing red to off. Create the class structures surrounding each of these classes, including all appropriate methods, properties, UML representation of the classes, and a class for testing the system.
  4. Create the class RationalNumber. The class will represent rational numbers by an int numerator and an int denominator. The class will allow the operations addition, subtraction, division, and multiplication. The class will be a subclass to the class Number and implement the interface Comparable.
<<<< Prev     Next >>>>