----------------------------------------------------- JAVA INTERFACES Overview: - Motivation - Syntax and semantics - Interface constants and methods - Extending interfaces - Marker interfaces ------------------------------------------------------- MOTIVATION Q: Why is it important to think about the design and the implementation separately? - Ideas in an early form: abstract data types (We will study them more closely later) and their algebraic specifications -------------------------------------------------------- MOTIVATING EXAMPLE - A variety of drawing elements > points, lines, triangles, circles, rectangles, squares, ellipses, et - How do we draw all such elements uniformly? Show code* public class Point{ int x; int y; public Point(){ x = 0; y = 0; } public int getX(){return x;} public int getY(){return y;} public void setX(int x){this.x = x;} public void setY(int y){this.y = y;} public void draw(){ System.out.println(this.toString()); } public String toString(){ return "(" + x + "," + y + ")"; } } public class Line{ Point p1; Point p2; public Line(Point p1, Point p2){this.p1 = p1; this.p2 = p2;} public int getP1(){return p1;} public int getP2(){return p2;} public void setP1(Point p1){this.p1 = p1;} public void setP2(Point p2){this.p2 = p2;} public void draw(){ System.out.println("[" + p1.toString() + "=>" + p2.toString() + "]"); } } Alternative: Abstract class FigureElement - provides an abstract draw method - point, line, etc implement the interface - all FigureElements provide a concrete implementation What about multiple inheritance? ------------------------------------------------------- MULTIPLE INHERITANCE Explain: Why helpful? What are some problems? ------------------------------------------------------ DO INTERFACES PROVIDE MULTIPLE INHERITANCE? - Is it easier to talk about interface-based multiple inheritance compared to class-based multiple inheritance? Mention Mix-ins briefly (Will come back to it). Mention Open-classes briefly (Will come back to it). Mention Hyperspaces briefly (Will come back to it). ------------------------------------------------------