160 likes | 325 Views
CS100J 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. Interfaces.
E N D
CS100J 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 Lecture 26
Interfaces • 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 interfaceinterface-name { list-of-method-signatures } • Extended syntax of classes class class-name1 extends class-name2 implements list-of-interface-names { . . . } Lecture 26
Interface 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 • etc. Lecture 26
An implementation of Comparable class Color implements Comparable { private int c; public Color(String s) { if (s.equals("red")) c = 0; elseif (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"; } public int compareTo(Object obj) { return c-((Color)obj).c; } } Lecture 26
Recall the sort method /* Sort A into non-decreasing order. */ staticvoid sort( int[] A ) { int m = A.length - 1; for ( int k = 0; k < m; k++ ) { /* Given that A[0..k-1] is finished, finish A[0..k]. */ int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ minLoc = k; for (int i=k+1; i <= m; i++) if ( A[i] < A[minLoc] ) minLoc = i; /* Exchange A[k] and A[minLoc] */ { int temp = A[k]; A[k] = A[minLoc]; A[minLoc] = temp; } } } Lecture 26
A polymorphic sort method /* Sort A into non-decreasing order. */ staticvoid sort( Comparable[] A ) { int m = A.length - 1; for ( int k = 0; k < m; k++ ) { /* Given that A[0..k-1] is finished, finish A[0..k]. */ int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ minLoc = k; for (int i=k+1; i <= m; i++) if (A[i].compareTo(A[minLoc])< 0 ) minLoc = i; /* Exchange A[k] and A[minLoc] */ { Comparable temp = A[k]; A[k] = A[minLoc]; A[minLoc] = temp; } } } Lecture 26
Sorting an array of Colors /* Print the elements of an arbitrary array of objects. */ staticvoid printArray(Object[] A) { System.out.println("-------------"); for (int i=0; i < A.length; i++) System.out.println(A[i] + " "); System.out.println("-------------"); } Color[] C = { new Color("blue"), new Color("white"), new Color("red") }; sort(C); printArray(C); • Output: ------------- red white blue ------------- Lecture 26
Sorting 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 • InprintArray(B),Integer is a subclass of Object • Interface implementations • In Sort(B), Integer implements Comparable Lecture 26
Polygon Quadrilateral Triangle Trapezoid Parallelogram Isosceles Rhombus Equilateral Square Taxonomy of Polygons Lecture 26
Parallelogram Rhombus Square A taxonomy of Quadrilaterals Parallelopoid : opposite sides parallel Paralleloid: opposite sides equal length Rhomboid: all sides equal length Rectangle Lecture 26
Parallelogram Rhombus Square A different taxonomy of Quadrilaterals Parallelopoid : opposite sides parallel all angles equal opposite angles equal Rectangle Lecture 26
Interface example: Shape // Objects with area and perimeter. interface Shape { public double area(); public double perimeter(); } Lecture 26
A class can implement multiple interfaces import java.lang.reflect.*; abstractclass Parallelopoid implements Shape, Comparable { // Compare two shapes based on areas. public int compareTo(Object obj) { double d = area() -((Shape)obj).area(); return (d < 0) ? -1 : (d > 0) ? +1 : 0; } public String toString() { return getClass().getName() + ":" + " area: " + (float)area() + " perimeter: " + (float)perimeter(); } } Lecture 26
class Rhomboid abstractclass Rhomboid extends Parallelopoid { double side; public double perimeter() { return 4*side; } Rhomboid(double side) { this.side = side; } } class Square extends Rhomboid { Square(double side) { super(side); } public double area() { return side*side; } } class Rhombus extends Rhomboid { double theta; Rhombus(double side, double theta) { super(side); this.theta = theta; } public double area() { return side*side*Math.sin(theta); } } Lecture 26
class Paralleloid abstractclass 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 side1*side2; } } class Parallelogram extends Paralleloid { double theta; Parallelogram ( double side1, double side2, double theta ) { super(side1, side2); this.theta= theta; } public double area() { return side1 * side2 * Math.sin(theta); } } Lecture 26
Sorting 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 ------------- Lecture 26