Note hw3 is available, due the Thurs after break.
Solutions to previous assignments are available on the class web page.
P2 demo last time: see fixed up files in p2 files area under “HostName” names.
We have been writing down APIs as a compact representation of the behavior of a class. Java has a similar construct, though it doesn’t have the constructors listed, just public methods. For example
API for Automobile
Automobile(String make, String model, int year, String
vehicleID)
String getMake() car’s make
String getModel() car’s model
int getYear() car’s model year
String getVIN() car’s vehicle ID
Quick example of use:
Automobile car = new Automobile(“Toyota”, “Camry”, 2011, “1GH1234567”);
System.out.println(“car’s year is “+ car.getYear());
Suppose we want to work with Automobiles and Trucks, and have a Truck type like this:
API for Truck
Truck(String make, int truckClass, String vehicleID)
//avoiding “class”
String getMake() truck’s make
int getClass()
truck’s class (1 to 8)
String getVIN() truck’s vehicle ID
Quick example of use:
Truck truck = new Truck(“Toyota”, 3, “1GH1234567”);
System.out.println(“truck’s class is “+ truck.getClass());
Now we see that our cars and trucks both have makes, and VINs. We can capture this commonality and use it in programs.
public interface Vehicle {
String getMake();
String getVIN();
};
Note: we can put “public” in front of “String getMake()”, etc., but it’s not needed, since methods must be public to be in an interface.
Note the lack of constructors, fields, and method bodies: it’s like an API, made up of method headers, but in this case without any constructors.
This declaration creates a new type called Vehicle, and we can say:
A Car IS-A Vehicle
A Truck IS-A Vehicle
To state this in Java:
public class Automobile implements Vehicle {
… just as before
}
public class Truck implements Vehicle {
//Truck fields, constructor(s), methods,
}
Example of use of Vehicle: We can’t do new Vehicle(…), no constructors. But we can create Automobiles/Trucks and call them Vehicles.
Remember, a Truck IS-A Vehicle, so there is “type compatibility”
Vehicle v1 = new Automobile(“Toyota”, “Camry”, 2011, “1GH1234567”);
Vehicle v2 = new Truck(…);
Now we can use the interface methods on v1 and v2
System.out.println(“ v1 has make “ + v1.getMake());
System.out.println(“ v2 has make “ + v2.getMake());
The power of this shows up more if we put these Vehicle objects in an array:
Although we can’t “new” a Vehicle, we can “new” a Vehicle array (it’s just a sequence of reference slots, no Vehicle objects)
Vehicle[] vehicles = new Vehicle[10];
vehicles[0] = v1;
vehicles[1] = v2;
…
End up with 10 cars and/or trucks. We can count the Fords across cars and trucks, look for a certain VIN, etc.
<picture of array of refs, with Automobile and Truck objects hanging off of it>
int countFords(Vehicle[] vehicles) {
int count = 0;
for (int i = 0; i < vehicles.length; i++) }
if (vehicles[i].getMake().equals(“Ford”)) {
count++;
}
}
return count;
}
Now look at text example: Shape interface
Idea--
A Circle IS-A Shape
A Rectangle IS-A Shape
A Triangle IS-A Shape
Common things we can do with shapes: find their area, perimeter.
public interface Shape {
double getArea();
double getPerimeter();
}
public class Circle implements Shape {
//Circle fields: just radius
//Circle constructor
// Circle methods: getArea(), getPerimeter() to satisfy interface
}
public class Rectangle implements Shape {
// Rectangle fields: width, height
// Rectangle constructor
// Rectangle methods: getArea(), getPerimeter() to satisfy interface
}
// similarly for Triangle
Look at type hierarchy on pg. 601
Note: change area() to getArea(), perimeter to getPerimeter() in this picture from the first edition
Here the dashed lines with hollow arrow head are used for “implements”, compared to solid lines and solid arrowhead used for “inheritance”, which we have skipped over.
We recognize the lower box list as the API without return types. Some people include the return types here.