Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 5a – Interfaces Friends share all things. CISC370/Object Oriented Programming with Java.

Similar presentations


Presentation on theme: "Topic 5a – Interfaces Friends share all things. CISC370/Object Oriented Programming with Java."— Presentation transcript:

1 Topic 5a – Interfaces Friends share all things. CISC370/Object Oriented Programming with Java

2 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Use and Distribution Notice Possession of any of these files implies understanding and agreement to this policy. The slides are provided for the use of students enrolled in Jeff Six's Object Oriented Programming with Java class (CISC 370) at the University of Delaware. They are the creation of Mr. Six and he reserves all rights as to the slides. These slides are not to be modified or redistributed in any way. All of these slides may only be used by students for the purpose of reviewing the material covered in lecture. Any other use, including but not limited to, the modification of any slides or the sale of any slides or material, in whole or in part, is expressly prohibited. Most of the material in these slides, including the examples, is derived from multiple textbooks. Credit is hereby given to the authors of these textbook for much of the content. This content is used here for the purpose of presenting this material in CISC 370, which uses, or has used, these textbooks.

3 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interfaces An interface is not a class, but a set of requirements for classes to conform to, if they want to implement the interface. The best way to explain this is with an example. We have seen before how to make an array of Employee object variables. In order to sort an array, we can call the Arrays.sort() method, a method of the Arrays class (used to perform functions on arrays).

4 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interfaces This method has the ability to sort arrays of any object class. In order to sort a group of things, there must be a way to decide if one object is less than, greater than, or equal to another object. So, there is one requirement on this, the class of objects we have an array of must be comparable. Comparable is an interface… public interface Comparable { int compareTo(Object other); }

5 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interfaces This is an interface. It simply states that any object that is Comparable (implements this interface) must have a method named compareTo(), which takes an Object as a parameter and returns an integer. public interface Comparable { int compareTo(Object other); }

6 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Implementing an Interface To make a class implement an interface, two steps need to be done: In the class definition, you need to specify that the class will implement the specific interface. You provide a definition, in the class, for all of the methods specified in the interface. Notice that an interface is very similar to an abstract (or virtual) class in C++…it is just a set of requirements that any implementing class must have.

7 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Implementing an Interface To make our Worker class Comparable… class Worker implements Comparable {... public int compareTo(Object otherObject) { Worker other = (Worker)otherObject; if (salary < other.salary)return –1; if (salary > other.salary)return 1; elsereturn 0; }

8 CISC370 – Object Oriented Programming with Java / © 2003 J. Six The Comparable Interface Looking up the Comparable interface in the API documentation reveals what the compareTo() method should do: Return a –1 if the first object is less than the second object (passed as a parameter) Return a 1 if the second object (passed as a parameter) is less than the first object) Return a 0 if the two objects are equal We can look this interface up in the docs because it’s a system interface – just like we can only look us Sun’s classes in the docs.

9 CISC370 – Object Oriented Programming with Java / © 2003 J. Six The Comparable Interface So, as written, what is the basis for deciding the <> relationship between Worker objects? class Worker implements Comparable {... public int compareTo(Object otherObject) { Worker other = (Worker)otherObject; if (salary < other.salary)return –1; if (salary > other.salary)return 1; elsereturn 0; }

10 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Testing for Interfaces We have seen the instanceof operator to see if an object is an instance of a particular class. We can also use this operator to see if an object implements a particular interface… if (obj1 instanceof Comparable) { // runs if whatever class obj1 is an instance of // implements the Comparable interface } else {// runs if it does not implement the interface }

11 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Testing for Interfaces So, in this manner we can find out if a particular object can be compared to another object in the manner described by the Comparable interface. if (obj1 instanceof Comparable) { // runs if whatever class obj1 is an instance of // implements the Comparable interface } else {// runs if it does not implement the interface }

12 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interface Object Variables We can use an object variable to refer to an object of any class that implements an interface. Using this object variable, we can only access methods that are present in the interface. For example… Object obj1; … if (obj1 instanceof Comparable) { Comparable comp1 = (Comparable)obj1; int res = comp1.compareTo(obj2); }

13 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interface Definitions Let’s look at the interface definition. Notice we do not specify the methods as public. For an interface, all methods are public by default. We do not need to explicitly specify them as public. public interface Comparable { int compareTo(Object other); }

14 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interface Definitions and Inheritance We can also extend interfaces, just like we can extend classes. This allows a chain of interfaces that go from general to more specific with each step. For example, let’s define an interface for a object which is capable of moving: public interface Movable { void move(double x, double y); }

15 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interface Definitions and Inheritance Here, we can see that a powered vehicle is also movable. In addition to all the requirements of being Movable, it must also have a MPG() method, which will return the miles per gallon this object gets. public interface Powered extends Movable { double miles_per_gallon(); }

16 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Constants in an Interface If a variable is specified in an interface, it is automatically a constant, specifically a public static final variable. Thus, an object that implements this new Powered interface has a constant, SPEED_LIMIT defined. public interface Powered extends Movable { double miles_per_gallon(); double SPEED_LIMIT = 95; }

17 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interface Definitions and Inheritance Thus, the Powered interface extends the Movable interface. Bear in mind, that any object that implements the Powered interface must satisfy all the requirements of that interface as well as its superinterface. So, a Powered object must have an MPG() method and a move() method.

18 CISC370 – Object Oriented Programming with Java / © 2003 J. Six Multiple Interfaces It is possible to implement multiple interfaces. As an interface is simply a promise to implement certain methods, we can easily have more than one interface but fulfilling the requirements of each one. A class can implement as many interfaces as it likes. Note this is NOT possible with inheritance…a class can only extend (or inherit from) one class.


Download ppt "Topic 5a – Interfaces Friends share all things. CISC370/Object Oriented Programming with Java."

Similar presentations


Ads by Google