T Topic 7 i 7 Interfaces and Abstract Interfaces and Abstract Classes Interfaces Interfaces “I prefer Agassiz in the abstract, rather than in the concrete.” CS 307 Fundamentals of CS 307 Fundamentals of 1 2 Computer Science Interfaces and Abstract Classes Computer Science Interfaces and Abstract Classes Multiple Inheritance Problems with Multiple Inheritance � Suppose multiple inheritance was allowed � S lti l i h it ll d � The are classes where the “is-a” test is true public class GradTA extends Faculty, GradStudent for more than one other class � Suppose Faculty overrides toString and that � Suppose Faculty overrides toString and that – a graduate teaching assistant is a graduate GradStudent overrides toString as well students GradTA ta1 = new GradTA(); GradTA ta1 = new GradTA(); – a graduate teaching assistant is a faculty System.out.println( ta1.toString() ); member � What is the problem What is the problem � Java requires all classes to inherit from � Certainly possible to overcome the problem exactly one other class – provide access to both (scope resolution in C++) – provide access to both (scope resolution in C++) – does not allow multiple inheritance – require GradTA to pick a version of toString or – some object oriented languages do override it itself (Eiffel) ( ) CS 307 Fundamentals of 3 CS 307 Fundamentals of 4 Computer Science Interfaces and Abstract Classes Computer Science Interfaces and Abstract Classes
A List Interface Interfaces – Not quite Multiple Inheritance � Java does not allow multiple inheritance � What if we wanted to specify the operations for a List, but no implementation? – syntax headaches not worth the benefits � Allow for multiple, different implementations. � Java has a mechanism to allow specification of a data type with NO implementation yp p � Provides a way of creating abstractions. o des a ay o c eat g abst act o s – interfaces – a central idea of computer science and programming. p g g � Pure Design Pure Design – specify "what" without specifying "how" – allow a form of multiple inheritance without the possibility of conflicting implementations possibility of conflicting implementations – "Abstraction is a mechanism and practice to Abstraction is a mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time. " CS 307 Fundamentals of CS 307 Fundamentals of 5 6 Computer Science Interfaces and Abstract Classes Computer Science Interfaces and Abstract Classes Interface Syntax Interfaces public interface List{ i i i � All methods in interfaces are public and public void add(Object val); abstract public int size(); – can leave off those modifiers in method headers public Object get(int location); � No constructors public void insert(int location, � No instance variables Object val); � can have class constants � can have class constants public void addAll(List other); public static final int DEFAULT_SIDES = 6 public Object remove(int location); } CS 307 Fundamentals of 7 CS 307 Fundamentals of 8 Computer Science Interfaces and Abstract Classes Computer Science Interfaces and Abstract Classes
Implementing Interfaces Why interfaces? � A class inherits (extends) exactly one other � I � Interfaces allow the creation of abstract data types f ll h i f b t t d t t class, but … – "A set of data values and associated operations that are precisely specified independent of any particular precisely specified independent of any particular � A class can implement as many interfaces implementation. " as it likes – multiple implementations allowed � Interfaces allow a class to be specified without public class ArrayList implements List worrying about the implementation � A class that implements an interface must A class that implements an interface must – do design first provide implementations of all method – What will this data type do? declared in the interface or the class must be declared in the interface or the class must be – Don’t worry about implementation until design is done. D ’t b t i l t ti til d i i d abstract – separation of concerns � interfaces can extend other interfaces interfaces can extend other interfaces � allow a form of multiple inheritance � allow a form of multiple inheritance CS 307 Fundamentals of CS 307 Fundamentals of 9 10 Computer Science Interfaces and Abstract Classes Computer Science Interfaces and Abstract Classes The Comparable Interface Comparable Interface version 1.4 � The Java Standard Library package java.lang contains a number of interfaces – names are italicized in the class public interface Comparable listing { public int compareTo( Object other ); public int compareTo( Object other ); � One of the most important } interfaces is the Comparable interface � compareTo should return an int <0 if the calling object is less than the parameter, 0 if they are object is less than the parameter, 0 if they are equal, and an int >0 if the calling object is greater than the parameter CS 307 Fundamentals of 11 CS 307 Fundamentals of 12 Computer Science Interfaces and Abstract Classes Computer Science Interfaces and Abstract Classes
Example compareTo Implementing Comparable � Any class that has a natural ordering of its � Suppose we have a class to objects (that is objects of that type can be model playing cards sorted based on some internal attribute) – Ace of Spades, King of Hearts, should implement the Comparable interface Two of Clubs � each card has a suit and a � Back to the ClosedShape example value, represented by ints � Suppose we want to be able to sort Suppose e a o be ab e o so � this version of compareTo will ClosedShape s and it is to be based on area compare values first and then p break ties with suits CS 307 Fundamentals of CS 307 Fundamentals of 13 14 Computer Science Interfaces and Abstract Classes Computer Science Interfaces and Abstract Classes compareTo in a Card class Interfaces and Polymorphism � Interfaces may be used as the data type public class Card implements Comparable { for object variables public int compareTo(Object otherObject) { { C Card other = (Card)otherObject; d th (C d) th Obj t � Can’t simply create objects of that type int result = this.myRank - other.myRank; if(result == 0) � Can refer to any objects that implement the � Can refer to any objects that implement the result = this.mySuit - other.mySuit; result = this mySuit - other mySuit; return result interface or descendants } // ot e // other methods not shown et ods ot s o � Ass me C � Assume Card implements Comparable d implements C bl } Card c = new Card(); Comparable comp1 = new Card(); Assume ints for ranks (2, 3, 4, 5, 6,...) and suits (0 is Comparable comp2 = c; clubs, 1 is diamonds, 2 is hearts, 3 is spades). CS 307 Fundamentals of 15 CS 307 Fundamentals of 16 Computer Science Interfaces and Abstract Classes Computer Science Interfaces and Abstract Classes
Polymorphism Again! What can this Sort? What can this Sort? public static void SelSort(Comparable[] list) { Comparable temp; p p int smallest; for(int i = 0; i < list.length - 1; i++) Abstract Classes Abstract Classes { small = i; { ; for(int j = i + 1; j < list.length; j++) { if( list[j].compareTo(list[small]) < 0) small = j; ll j Part Class, part Interface } // end of j loop temp = list[i]; list[i] = list[small]; list[small] = temp; } // end of i loop } CS 307 Fundamentals of CS 307 Fundamentals of 17 18 Computer Science Interfaces and Abstract Classes Computer Science Interfaces and Abstract Classes The ClosedShape class Back to the ClosedShape Example � One behavior we might want in public class ClosedShape { private double myX; ClosedShapes is a way to get the area private double myY; � problem: How do I get the area of something public double getArea() { //Hmmmm?!?! that is “just a ClosedShape”? } // } // Other methods not shown Doesn’t seem like we have enough information to get the area if all we know is it is a ClosedShape. CS 307 Fundamentals of 19 CS 307 Fundamentals of 20 Computer Science Interfaces and Abstract Classes Computer Science Interfaces and Abstract Classes
Recommend
More recommend