Topics:
Public vs. Private vs. Protected (brief explanation)
Interfaces and Abstract Classes
Sample Project: StringDrawer.zip
Question: Imagine that the class is a team of programmers. We want to write a karel program that can take a string of text, and write out in beepers in the world. How could we do this?
Well, we could try to write one big method that does the whole thing, but that doesn’t really utilize the team of programmers.
A better solution would be to assign each person in the class two letters that they were in charge of writing code for. Then someone else could write code to parse the string and call the methods that everyone else is writing…but this assumes that all of the methods will be written according to some agreed upon convention.
How do you enforce such a convention?
Answer: an interface. An interface defines a set of methods that a class must implement.
Public interface LetterDrawer{
Public void draw(int st, int ave);
}
public class HBot implements LetterDrawer{
public void draw(int st, int ave){
//code that draws the letter.
}
}
By implementing an interface you are guaranteeing that your class will provide implementations for certain methods, in this case draw(…).
Once we define the LetterDrawer interface someone can safely write code that uses LetterDrawers since they know which methods will be provided. (STRONGER EXAMPLE BELOW).
Abstract classes are slightly different. They provide implementations for some methods but not all. Abstract classes are extended like a normal object, but they won’t compile unless you provide code for the abstract method.
EXAMPLE (redo LetterDrawer as an abstract class):
public abstract class LetterDrawer{
public abstract void draw(int st, int ave);
protected void drawLine(Robot R){
while(R.anyBeepersInBeeperBag()){
R.putBeeper();
R.move();
}
}
}
public class HBot extends LetterDrawer{
public void draw(int st, int ave){ //notice no “abstract” here
// code that draws the H
}
}
So yesterday I told you what Interfaces and Abstract classes were, but it was a little unclear why I was telling you about them, or why you’d want such things.
Here’s an attempt at explaining a reason that’s a little beyond the scope of what we’ve learned so far, but here goes…
Java comes with many pre-written classes and utilities. One such utility is the sort method of the Arrays class. We haven’t really learned about arrays or sorting, yet, but you can imagine that an array is a collection of Objects. The sort method will attempt to sort the Objects in the collection.
But what is a pre-condition of sorting? That the objects are somehow comparable. In order to sort, something must be able to determine if one object should come before another in some order.
Well, how can Java guarantee that the sort method will work for any object? Not all Objects have an obvious way to be ordered. What if we had an array of Robots for example?
Well, first you have to define how Robots could be ordered (make something up). Then you’d have to communicate that to the sorting method somehow…ultimately inside the sorting method, it will look at many pairs of Objects and have to decide whether to put one in front of the other…..or you could write your own method to sort.
Well, the answer is that the sort method doesn’t guarantee that it can sort *anything*. It guarantees that it can sort any object that is Comparable. As such, Java provides an interface called Comparable. The sort method will sort anything that implements the Comparable interface.
The code looks like this:
public interface Comparable{
public int compareTo(Object o);
}
By implementing the Comparable interface the sort method knows that each object in the array will have a method called compareTo that it can use to determine order.
This allows you to write the sort method ONCE - just sort Comparable objects. Then anyone who has some goofy object that they want sorted will have to implement the Comparable interface (correctly) if they want to use the built in sorting utility.
Thus, interfaces are created and used to provide a template for an object without having to worry about its implementation. From the Java API:
public static void sort(Object[] a)
Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface.
The same is basically true for Abstract classes and methods. The difference here is that the code can provide some, but not all of the implementation for an Object.
SAMPLE PROJECT: ComparableTest.zip
In class you wrote implementations for the DigitDrawer interface — you should have this project among your APCS work. I then had you give me all of the .class files of the implementations you wrote and I used them to be able to draw a multi-digit number in the karel world.
Leave a Reply