This includes the examples from Walter Savitch's Chapter 8 & Barron's chapter on Inheritance and Polymorphism.
Slide 2
8.4 Interfaces and Abstract Classes
Here is an example of a Java Interface.An interface is a program that contains the headings for a number of public methods. Some interfaces describe all the public methods in a class, while others specify only certain methods. An interface also can define CONSTANTS.A Java interface begins like a class definition, but uses "interface" instead of "class."THERE ARE NO CONSTRUCTORS DECLARED BY INTERFACES. Methods within must be PUBLIC so you can omit PUBLIC from their headings.THERE ARE ALSO NO INSTANCE VARIABLES, OR COMPLETE METHOD DEFINITIONS.
public interface Measurable { public double getPerimeter(); public double getArea();}public interface Interface_Name { Public_Named_Constant_Definitions ... Public_Method_Heading_1; ... Public_Method_Heading_n;}
Slide 3
Implementing Interfaces
To implement an interface, a class must:1. Include the phrase implements Interface_Name at the start of the class definition. YOu can implement more than one interface using implements MyInterface, YourInterface.2. Define EACH METHOD DECLARED IN INTERFACE. If, for example, we write public class REctangle implements Measurable, the class must also implement the two methods getPerimeter and getArea.If you have a circle, and you use "getCircumference", it will call getPerimeter instead.
Measurable m;Rectangle box = new Rectangle(5.0, 5.0);m = box;display(m);Circle disc = new Circle(5.0);m = disc;display(m);
The invocation of display(box) shows Perimeter = 20.0; area = 25.0. The invocation of display(disc); displays Perimeter = 31.4 and area = 78.5.The classes Rectangle and Circle implement the same interface, so we are able to substitute an instance of one for an instance of the other when we call the method display. The code on the left displays just the same output.
Slide 5
Dynamic Binding & Interfaces
Measurable m = new Circle (5.0);System.outprintln(m.getCircumference()); ILLEGALMeasurable m = new Circle (5.0);Circle c = (Circle)m;System.out.println(c.getCircumference());
We can assign an object of type Circle to a variable of type Measurable, again because Circle implements Measurable. However, we can set the variable to invoke ONLY A METHOD IN INTERFACE MEASURABLE!!! This is illegal,!!! getCircumference is not the name of a method in the MEASURABLE INTERFACE. Even though m is of type Measurable and is an object of type Circle, the compiler does not allow it to use the getCircumference method. We can only cast this for it to work.A VARIABLE'S TYPE DETERMINES WHAT METHOD NAMES CAN BE USED, HOWEVER, THE OBJECT THE VARIABLE REFERENCES DETERMINES WHICH DEFINITION OF THE METHOD WILL BE USED.
Slide 6
More on Polymorphism
Student s = null;
Student u = underGrad(“Tim Broder”, new int[] {90,90,100}, “none”);
Student g = new GradStudent(“Kevin Cristella”, new int[] {85, 70, 90}, “none”, 1234);
System.out.print(“Enter student status:”);System.out.println(“Grad G, Undergrad U, Neither N”);String str = IO.readString();if (str.equals(“G)) {s = g;}
else if (str.equals(“U”)) {s = u;}
else {s = new Student;}
s.computeGrade();
When this code fragment is run, the computeGrade method used will depend on the type of the actual object s refers to, which in turn depends on the user input.
Slide 7
public class StudentTest {
public static void computeAllGrades (Student[] studentList) {
for (Student s: studentList)
if (s != null) {s.computeGrade();}
} public static void main (String[]args) {
Student[] stu = new Student[5];
stu[0] = new Student (“Brian Lorenzen”, new int[]
{90,94,99}, “none”); stu[1] = new UnderGrad (“Tim Broder”, new int[] {90,90,100}, “none”); stu[0] = new GradStudent (“Kevin Cristella”, new int[] {85,70,90}, “none”,1234);
computeAllGrades(stu);
}
}
Here, an array of five Student references is created, all of them initially null. Three of these references, stu[0], stu[1], and stu[2], are then assigned to actual objects. The computeAllGrades method steps through the array invoking for each of the objects the appropriate computeGrade method, using dynamic binding in each case. The null test in computeAllGrades is necessary because some of the array references could be null.
Slide 8
public class Dancer {
public void act() {
System.out.print(“ spin”);
doTrick();}
public void doTrick() {
System.out.print(“ float”);}
} public class Acrobat extends Dancer {
public void act() {
super.act();
System.out.print(“ flip”); }
public void doTrick() {
System.out.print(“ somersault”);
}
}
Suppose the following declaration appears: Dancer a = new Acrobat();
What is printed as a result of the call a.act()?
The act method of ACROBAT is executed!
This is an example of polymorphism. The first line, super.act(), goes to the act method of Dancer, the superclass. This prints spin, then calls doTrick().
Again, using polymorphism, the doTrick method in Acrobat is called and somersault is printed. Now, completing the act method of Acrobat, flip is printed. So the output is spin somersault flip.
8.4 Interfaces & Abstract Classes