Placeholder Image

Subtitles section Play video

  • An interface is kind of like a club.

  • Here’s a picture of a club in New York City.

  • Members of the club are allowed to

  • attend their events and use their fitness center.

  • They also have a membership card,

  • which allows them to prove they are a member.

  • Nonmembers, well, they can admire the view of the exterior.

  • As you might guess, membership in this club isn’t free

  • members pay dues for their privileges.

  • For Java interfaces, the dues are a list of methods

  • a class is required to implement.

  • Here we see an interface Vehicle with three methods,

  • start, stop and move.

  • I’ve explicitly labeled these methods as public.

  • All interface methods are both public and abstract

  • whether or not you specify those keywords.

  • To be a member of Vehicle,

  • a class must implement these three methods

  • those are the dues it pays.

  • The Java keyword for being a member of an interface isimplements”.

  • Here we see a class Car, that implements the interface Vehicle.

  • It must have the three methods, “start”, “moveandstop”,

  • and these methods must be public.

  • Once it haspaid its duesby implementing these three methods,

  • it gets all of the privileges of being a Vehicle.

  • Anything that takes a Vehicle will accept a Car.

  • For example, I can pass car1 to the method travel.

  • I can also cast a variable of type Car to a variable of type Vehicle.

  • And, if I ask if car1 is an instance of Vehicle, the answer will be true.

  • Cars are card-carrying members of the club vehicle,

  • with all the rights and privileges pertaining thereto.

  • An interface might remind you of an abstract class.

  • It is similar.

  • Interfaces, like abstract classes,

  • can have abstract methods that other classes are forced to implement.

  • But, while a class can only have one parent class,

  • it can implement as many interfaces as it wants.

  • The other key difference is that

  • an abstract class can define methods and attributes

  • that are inherited by its children.

  • Interfaces may only contain constants and abstract methods

  • no code or attributes.

  • One of the most useful interfaces built into Java isComparable”.

  • To be comparable, there must be an ordering between objects.

  • That is, for any two objects,

  • we can determine if the first is less than, equal to, or greater than the second.

  • The compareTo method returns 0 if the objects are equal,

  • a negative number if the first is smaller and a positive number if the first is larger.

  • The reason for specifying just positive or negative

  • instead of -1 and 1 is that this allows us to simply do a subtraction

  • rather than an if statement in many cases.

  • The class OrderedPoint gives an example of implementing Comparable.

  • OrderedPoint is a child class of Point,

  • so it couldn’t also be a child class of a Comparable class.

  • But, since Comparable is an interface,

  • OrderedPoint can be a child class of Point and implement Comparable.

  • The less than OrderedPoint greater than

  • is present because Comparable is a generic class,

  • and we have to specify that we will

  • compare OrderedPoints to other OrderedPoints.

  • For our ordering, well use the distance to the origin.

  • Since we only interested in comparing distances,

  • we actually use the square of the distance

  • so we don’t have to bother with the square root.

  • The return value will be 0

  • if the two points are equidistant from the origin,

  • a negative number ifthisis closer to the origin

  • and a positive number otherwise.

  • Looking at PointsInOrder,

  • we see that the big win for implementing Comparable

  • is that we can use the method Arrays.sort.

  • If we pass an array of Comparable objects to Arrays.sort,

  • it will sort them in increasing order for us

  • using a tuned quicksort algorithm that should be very fast for most purposes.

  • There’s also a static binarySearch method in the Arrays class

  • that can be used to find items quickly in a sorted array.

  • Sorting and searching arrays are very common tasks,

  • and Comparable allows us to avoid having to rewrite those algorithms

  • over and over again.

  • Running the program, it generates 10 random points in the first quadrant

  • with x and y coordinates from 0 to 19,

  • sorts them in order of increasing distance from the origin,

  • and then prints them to System.out.

  • You can download the source code for OrderedPoint and PointsInOrder

  • from java.martincarlisle.com.

An interface is kind of like a club.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it