Download presentation
Presentation is loading. Please wait.
Published byScot Fitzgerald Modified over 9 years ago
1
CS2 Module 26 Category: OO Concepts Topic: Interfaces Objectives –Interfaces
2
CS 2 Introduction to Object Oriented Programming Module 26 OO Concepts Interfaces
4
Some languages allow multiple inheritance: Multiple inheritance leads to many potentially confusing naming problems (e.g. Pet.doYourThing() vs. Animal.doYourThing()) Growing consensus: the benefits of multiple inheritance aren’t worth the problems. Java has single inheritance only. No multiple inheritance Java has a construct that can give us much of the functionality of multiple inheritance: The Interface Animal Dog Pet Two parent classes, one child class No Multiple Inheritance in Java
5
What is an Interface? “An interface is a named collection of method definitions (without definitions). An interface can also declare constants.” – The Java Tutorial, Sun Microsystems Sounds similar to an abstract class, but has significant differences: 1.Interfaces cannot have any methods with implementation 2.A class can implement many interfaces, but can inherit from only one superclass 3.Interfaces are not part of a class hierarchy, so unrelated classes can implement the same interface. Purpose:Define a protocol of behavior that can be provided by any class. Any class claiming to “implement” a given interface is then contractually obligated to provide implementations of all the methods listed in the interface.
6
How Do Interfaces Relate to Multiple Inheritance? the class implementing an interface behaves as if it were inheriting from a parent class, only better: –it implements all of the abstract methods itself –there is no other hidden, inherited stuff to collide with (no name confusion) You can implement as many interfaces as you wish with no fear of the problems of multiple inheritance
7
Interface Example We want a container class that can sort the items it holds. What method will be used to compare two objects? Will we have such a method? We make a kind of specification, a protocol: –We promise that any object we want to store in this sorting container class will have a method allowing it to be compared to other objects. –Also that method will have a known signature. How do we formalize this? Use of an interface
8
Note to Instructors Change this so it uses something other than Comparable Some of the Comparable slides already have been copied to the Generic classes lecture
9
Interface java.lang.Comparable Comparable is a predefined interface that is part of Java. Straight from the API: “This interface imposes a total ordering on the objects of each class that implements it.” (Look it up yourself!) public interface Comparable { public int compareTo(Object o) } Returns: a negative integer, zero, or a positive integer as the calling object is less than, equal to, or greater than the object specified as a parameter.
10
Suppose... We want to store Box objects and for our purposes we decide to compare boxes based on their volume. We modify our Box class thusly:
11
Suppose... public class Box implements Comparable { // Same as before including getVolume method // but omitted for the sake of the PowerPoint slide public int compareTo(Object o) { int retval = -1; if(o instanceOf Box) { retVal = getVolume() - ((Box)o).getVolume(); if(retVal == 0) retVal = 0; else if(retVal > 0) retVal = 1; else retval = -1; } // if return retval; }
12
Running the code... Called like this: Box a = new Box(10, 20, 30); Box b = new Box(2, 4, 6); Box c = new Box(20, 10, 30); System.out.println(a.compareTo(b)); ===> 1 System.out.println(a.compareTo(c)); ===> 0 System.out.println(b.compareTo(c)); ===> -1 System.out.println(a.compareTo("Hello")); ===> - 1;
13
Don't be nervous! If we say that some object has to be “Comparable” it's like saying: It must be an object (that is, it must have an is-a relationship with java.lang.Object. It pretty much will satisfy that by default.), AND It must have a compareTo method implemented. (So just make sure you write a compareTo method that has the same signature of that found in the interface java.lang.Comparable Making your class implement the Comparable interface is just like making a contractual obligation to provide that method – the compareTo method.
14
Another use!!! Slick way to provide constants
15
We can create a regular class just for constants class Constants { public final static int FEETPERMILE = 5280; public final static String PROMPT = "Enter a number"; public final static double PI = 3.141592; } Sample usage: feet = miles * Constants.FEETPERMILE;
16
If instead we say interface Constants { public final static int FEETPERMILE = 5280; public final static String PROMPT = "Enter a number"; public final static double PI = 3.141592; } Sample usage: public class Box implements Constants { // lots of code omitted feet = miles * FEETPERMILE;
17
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.