Title: CS100J Lecture 26
1CS100J Lecture 26
- Previous Lecture
- Application of inheritance
- Higher-order" methods
- Abstract classes
- Visibility modifier protected
- This Lecture
- Interfaces
- Comparable
- Reflection
- super
- Reading
- Lewis Loftus, Section 5.5
- Savitch, Appendix 7
2Interfaces
- Interface - Like an abstract class
- A collection of abstract methods that must be
defined by any class that implements the
interface. - Primitive types do not implement interfaces
- Syntax of interfaces
- interface interface-name
-
- list-of-method-signatures
-
- Extended syntax of classes
- class class-name1
- extends class-name2
- implements list-of-interface-names
-
- . . .
3Interface example Comparable
- // Linearly ordered objects.
- interface Comparable
-
- / Compare this object with obj
- - if this before obj, return negative,
- - if this equals obj, return 0,
- - if this after obj, return positive. /
- public int compareTo(Object obj)
-
- The following predefined classes all implement
the Comparable interface - Byte
- Character
- Double
- Float
- Integer
- Long
- Short
- String
4An implementation of Comparable
- class Color implements Comparable
-
- private int c
- public Color(String s)
-
- if (s.equals("red")) c 0
- else if (s.equals("white")) c 1
- else if (s.equals("blue")) c 2
- else throw
- new RuntimeException("bad color")
-
- public String toString()
-
- if (c 0) return "red"
- else if (c 1) return "white"
- else return "blue"
-
5Recall the sort method
- / Sort A into non-decreasing order. /
- static void sort( int A )
-
- int m A.length - 1
- for ( int k 0 k lt m k )
-
- / Given that A0..k-1 is finished,
- finish A0..k. /
- int minLoc // subscript of
- // smallest in Ak..m
- / Set minLoc so AminLoc is
- smallest in Ak..m. /
- minLoc k
- for (int ik1 i lt m i)
- if ( Ai lt AminLoc )
- minLoc i
- / Exchange Ak and AminLoc /
6A polymorphic sort method
- / Sort A into non-decreasing order. /
- static void sort( Comparable A )
-
- int m A.length - 1
- for ( int k 0 k lt m k )
-
- / Given that A0..k-1 is finished,
- finish A0..k. /
- int minLoc // subscript of
- // smallest in Ak..m
- / Set minLoc so AminLoc is
- smallest in Ak..m. /
- minLoc k
- for (int ik1 i lt m i)
- if (Ai.compareTo(AminLoc)lt 0 )
- minLoc i
- / Exchange Ak and AminLoc /
7Sorting an array of Colors
- / Print the elements of an arbitrary array of
objects. / - static void printArray(Object A)
-
- System.out.println("-------------")
- for (int i0 i lt A.length i)
- System.out.println(Ai " ")
- System.out.println("-------------")
-
- Color C
- new Color("blue"),
- new Color("white"),
- new Color("red")
-
-
- sort(C) printArray(C)
- Output
8Sorting an array of Integers
- Integer B
- new Integer(3),
- new Integer(1),
- new Integer(2)
-
-
- sort(B) printArray(B)
- Output
- -------------
- 1
- 2
- 3
- -------------
- Note two kinds of polymorphism
- Inheritance hierarchy
- In printArray(B), Integer is a subclass of Object
9Taxonomy of Polygons
10A taxonomy of Quadrilaterals
Parallelopoid opposite sides parallel
Paralleloid opposite sides equal length
Rhomboid all sides equal length
Rectangle
11A different taxonomy of Quadrilaterals
Parallelopoid opposite sides parallel
all angles equal
opposite angles equal
Rectangle
12Interface example Shape
- // Objects with area and perimeter.
- interface Shape
-
- public double area()
- public double perimeter()
-
13A class can implement multiple interfaces
- import java.lang.reflect.
- abstract class Parallelopoid
- implements Shape, Comparable
-
- // Compare two shapes based on areas.
- public int compareTo(Object obj)
-
- double d area() -((Shape)obj).area()
- return (d lt 0) ? -1 (d gt 0) ? 1 0
-
-
- public String toString()
-
- return getClass().getName() ""
- " area " (float)area()
- " perimeter " (float)perimeter()
-
-
14class Rhomboid
- abstract class Rhomboid extends Parallelopoid
-
- double side
- public double perimeter()
- return 4side
- Rhomboid(double side)
- this.side side
-
- class Square extends Rhomboid
-
- Square(double side)
- super(side)
- public double area()
- return sideside
-
- class Rhombus extends Rhomboid
-
15class Paralleloid
- abstract class Paralleloid extends Parallelopoid
-
- double side1, side2
- Paralleloid( double side1, double side2)
- this.side1 side1 this.side2 side2
- public double perimeter()
- return 2(side1 side2)
-
- class Rectangle extends Paralleloid
-
- Rectangle(double side1, double side2)
- super(side1, side2)
- public double area()
- return side1side2
-
- class Parallelogram extends Paralleloid
-
16Sorting an array of Parallelopoids
- Parallelopoid A
- new Square(1.0),
- new Rhombus(1.0, Math.PI/4),
- new Rectangle(1.0, 2.0),
- new Parallelogram(1.0, 2.0, Math.PI/4),
-
-
- sort(A) printArray(A)
- Output
- -------------
- Rhombus area 0.70710677 perimeter 4.0
- Square area 1.0 perimeter 4.0
- Parallelogram area 1.4142135 perimeter 6.0
- Rectangle area 2.0 perimeter 6.0
- -------------