Today we will be learning about interfaces and inheritance in Java. You should create a directory called lab4 where you will save your work and gsubmit this directory when you are done with the lab.
We start with an interface given in ItemForSale.java. As the name suggests, this interface is for an object that represents an item that is sold at a store. An interface is not some cryptic computer science concept, it is something that you are very familiar with. For example, if you own a coffee maker you know that when you hit the "power" button the device will turn on, when you hit "brew" it will make the coffee, and it will pour a large or small cup depending on what "cup size" you select :) However, you do not need to be able to make a coffe maker, nor even understand how it works in order to use it. You just need to be able to interface with it. Similarly, interfaces in programming allow us to abstract from the details of a potentially large and complicated piece of code, and focus on its core functionality.
Back to our example, if you look at the ItemForSale interface you can see that an ItemForSale must implement the four methods that are listed there. Your first task is to define a class called Television that implements this interface. A Television object should have a description (String), a brand (String), a price (Double), a variable to determine whether it is in stock (boolean), and a variable to store the size of the screen (Double). It should also have a constructor that allows you to set these fields. Television should also implement all the methods listed in ItemForSale, and you should let the compiler know that it implements this interface by using the implements keyword when declaring the class. In addition, you need to add an accessor method for the screen size called getScreenSize().
Now you need to define an object that represents a specific type of Television, a class called FlatScreenTelevision. FlatScreenTelevision should do everything that Television does, and have an additional variable called resolution (Double), and an accessor method for it called getResolution().
Since we want a FlatScreenTelevision to do everything that Television does, instead of duplicating code we can take advantage of inheritance, which allows objects to share common functionality. When declaring FlatScreenTelevision you can use the extends keyword to let the compiler know that it is inheriting from Television.
Once you use the extends keyword, all of the methods and fields of the parent class become available to the child class (without you having to code anything). So all you need to do is declare a single variable called resolution, and an accessor method for it. You also need to have a constructor for FlatScreenTelevision that now takes six parameters. To set the first five fields, you can call the constructor of the parent class by using the keyword super.
In addition, FlatScreenTelevision should implement the Comparable<FlatScreenTelevision> interface. This interface is a specification that this class must implement the compareTo(FlatScreenTelevision) method, which allows us to compare two FlatScreenTelevision objects. If you look at the documentation of the Comparable interface in the Java API, you can see a description of what this method should do. To keep it short, if you call tv1.compareTo(tv2), where tv1 and tv2 are FlatScreenTelevision objects, it should return a negative integer if tv1 is "less" than tv2, 0 if they are equal, and a positive integet if tv2 is "greater." What "greater" and "less" means is up to us to define. In our exercise let's say that we want to order them by resolution.Once you are finished, you should be able to run lab4Client.java. Take a look at what is going on there. First we create four FlatScreenTelevision objects and put them in an array. Since FlatScreenTelevision implements the Comparable interface, we can use the Arrays.sort() method to sort these objects in the array. Then we go through the tv's one by one and print out their brand and price. Notice that inside the for-loop we refer to each FlatScreenTelevision as an ItemForSale. The compiler allows this because every FlatScreenTelevision is a Television, and every Television is an ItemForSale (because it implements the ItemForSale interface). However, once we cast from FlatScreenTelevision to ItemForSale, we can only use the methods defined in ItemForSale. Of course, we would not be allowed to cast in the other "direction" (for example from Television to FlatScreenTelevision). Also, since Object is the class that is at the top of the type hierarchy in Java, we can cast any type (excluding primitives like int) to Object.