Topic

Interfaces

Description

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.

Implementation

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:

  • In the class header, add the phrase: implements Status
  • Add the method getStatus ( ) in the body of the class.
    This method returns the status as a String. The string returned includes:
    The name of the class on one line.
    The name of all instance variables along with their values on a second line.
    For example, for an object of class Rectangle of length 3 and width 2, the String returned should be as below:
    "Rectangle\nLength=3, Width=2\n"
  • Add the method displayStatus ( ) in the body of the class. This method will call the method getStatus and display the status received

Create a class TestStatus containing the method main ( ). In the main method do the following:

  • Create an array stat of size 6 for Status references. (Compiler allows you to create such an array). This array will be populated with references of different objects later.)
  • Create three Rectangle objects, initialize them with data values provided by the user and store their references in the first 3 elements of the Status array. (You can do this because the class Rectangle implements the interface Status and thus a Rectangle object is both of type Rectangle and Status).
  • Create two Sibling objects, initialize them with data values provided by the user and store their references in the next two elements of the Status array. (You can do this because the class Sibling implements the interface Status and thus a Sibling object is both of type Sibling and Status).
  • Create one Student objects, initialize it with data values provided by the user and store its reference in the next one element of the Status array. (You can do this because the class Student implements the interface Status and thus a Student object is both of type Student and Status).
  • Set up a loop to iterate through the array of Status references. (Each reference in the Status array will point to an object). During each pass through the loop, call method displayStatus on a Status reference and it will display its status.
  • Set up a loop to iterate through the array of Status references. (Each reference in the Status array will point to an object). During each pass through the loop, Call the method getStatus ( ) to get the Status of each object as a String, and accumulate the output into a String variable out.
  • On exit from the loop, display the content of variable out in dialog box..

Testing

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

Sample Code

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);
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.