Interfaces
Problem in a Video Scene
Consider a video scene with many different types (classes) of objects to be displayed. We would like to provide a method display in all of these objects. We would like to store all the object references in a single array and then call the method display on all the objects in a loop so that these objects may display themselves. However, Java is a strongly typed language. If we create an array of type Rectangle, we can only store Rectangle object references in it. We cannot have an array in which we can store different type of objects. We encounter a similar problem in our assignment below.
Similar Problem in Assignment
In our assignment, we will create three Rectangle objects, two Sibling objects and one Students object. We would like to provide a method displayStatus and a method getStatus in each of these objects. We would like to put all the objects in one array and then, in a loop, call the method displayStatus on each of the objects so that they all may display their statuses as below.
for (int i=0; i<6; i++) {
objectArray [i].displayStatus();
}
Similarly, we would like to call their method getDisplay in a loop and receive their statuses as a String one by one. Then, we can gather their statuses and show them together in a single dialog box.
Using an Interface
In our assignment, we have a problem similar to the one described earlier in the video scene. We want to create an array in which we could store objects of different types. However, in Java, we can store objects of only one type in an array. So, we will solve this problem by making all our objects of a one common type. We will do this by creating an interface Status containing the headers of two methods displayStatus and getStatus. Then we will implement the interface Status in our classes Rectangle, Sibling and Student. This will require us to provide methods displayStatus and getStatus in those classes. and make the objects of all these classes to be of type Status in addition to their original type. Now that these objects of different types and have become also object of a one common type Status. We can store them in an array of type Status and the problem will be solved.
So, we will create an array of type Status and of size 6. We will be able to create an array of type Status because it will be an array of six Status references and not Status object. We will store references of three Rectangle objects, two Sibling objects and one Student object in Status array. We will be able to do that because having implemented the Status interface, these objects are of type Status in additional to their original type.
Essentially, we made objects of type Rectangle, Sibling and Student also of a common type Status. Having made them objects of a commom type Status, we are able to store their references in an array of type Status. Then, we are able to call their method displayStatus or getStatus in a loop as below:
for (int i=0; i<6; i++) {
status [i].displayStatus(); //Polymorphic call
}
Polymorphism and Dynamic Method Binding
Poly means multiple and morph means form. So, polymorphism means multiple forms. In the above loop, we call method displayStatus on variety of object types stored in status array such as Rectangle objects, Sibling objects and Student objects. So, it is polymorphic call.
In Java, the polymorphic calls work because, in Java, method calls, in general, are not bound at compile time. (The compiler does not figure out the jump address of the method call). Instead, in Java, method calls are bound at run time. For example, for a call such as status[i].displayStatus ( ), Java figures out at run time what type of reference status[i] is. If it is Rectangle reference, then it jumps to the Rectangle displayStatus method. If status[i] is a Sibling reference, it jumps to the Sibling displayStatus method and so on.
In brief, in Java, calling displayStatus method on different type of references stored in an array such as status works because Java employs run-time (dynamic) (delayed) method binding and not compile-time (static) method binding.
Assignment Steps
For doing this assignment, follow the steps below: (For details, refer to the Implementation section)
1. Create classes Rectangle, Sibling and Student or copy them from previous assignments.
2. Create an interface Status containing the headers of the methods displayStatus [public void displayStatus();] and getStatus [public String getStatus();]
3. Implementing the interface Status in each of the three classes: Rectangle, Sibling and Student. This would require you to add methods displayStatus and getStatus in each of these classes.
4. Create an array of type Status of size 6. (You will be able to create an array of type Status because you are just creating an array of references of type Status. It is not possible to create objects of type Status because it is an interface.)
5. Create three objects of class Rectangle, two of class Siblings, one of class Student and store their references in the above array of type Status. (Having implemented the interface Status, objects of these classes are of type Status in addition to their original types.)
6. In a loop, call the method displayStatus on each object. (Each will display its status.)
7. In a loop, call the method getStatus on each object. (Each will return its status as a String.)
8. Accumulate, in String variable out, the statuses returned from the above method calls.
9. Display the content of string variable out in a dialog box.
Create an interface Status containing the following method:
public void displayStatus ( );
public String getStatus ( );
Create a class Rectangle containing two private instance variables double length and double width. Provide a two-parameter constructor that will initialize the two instance variables. Implement the interface Status.
Create a class Sibling containing three private instance variables: String name, int age and int weight. Provide a three-parameter constructor that will initialize the three instance variables. Implement the interface Status.
Create a class Student containing three private instance variables: int id, String name and double array scores. Provide a three-parameter constructor that will initialize the three instance variables. Implement the interface Status.
In each of the above classes, implement the interface Status. For this purpose, do the following:
Create a class TestStatus containing the method main ( ). In the main method do the following:
For input/output, you may use JOptionPane.showInputDialog.
Input
Data for three Rectangle objects:
3 2
6 4
30 20
Data for two Sibling objects:
Jack 21 130
Judy 24 118
Data for one Student Object:
1,John Adam,3,93,91,100
Output
The output should show the status of each of the object one after the other as below.
Rectangle
length=3, width=2
Rectangle
length=6, width=4
Rectangle
length=30, width=20
Sibling
name=Jack, age=21, weight=130
Sibling
name=Judy, age=24, weight=118
Student
id=1, name=John Adam, scores=93, 91, 100
The sample code below can be used in the main method.
//First Polymorphic Loop
//Below assume that status is an array of Status references.
//Call displayStatus ( ) in a loop as shown below:
for (int index=0; index{
status[index].displayStatus ( );
}
//Second Polymorphic Loop
//Below assume that status is an array of Status references.
//Call getStatus ( ) in a loop and accumulate output as shown below.
//After exiting from the loop, display the accumulated output.
String out = “”;
for (int index=0; index{
out = out + status[index].getStatus ( );
}
JOptionPane.showMessageDialog (null, out);