<< Chapter < Page Chapter >> Page >

General background information

Among other things, this program illustrates:

  • Interface definitions
  • Implementing an interface in a class definition
  • Defining interface methods in a class definition
  • Storing references to new objects in elements of an array of type Object
  • Casting elements to an interface type in order to call interface methods
  • Parameterized constructors
  • Overridden toString method

Discussion and sample code

Will explain in fragments

I will explain this program in fragments. A complete listing of the program is provided in Listing 13 near the end of the module.

Beginning of driver class for Prob05

The driver class for Prob05 begins in Listing 1 .

Listing 1 . Beginning of driver class for Prob05.
import java.util.*; class Prob05{public static void main(String[] args){Random generator = new Random(new Date().getTime()); int randomData = (byte)generator.nextInt();Object[] var1 = new Object[2]; var1[0]= new Prob05MyClassA(randomData); var1[1]= new Prob05MyClassB(randomData);

Behavior of the code in Listing 1

Listing 1 does the following:

  • Gets and saves a random value of type int .
  • Instantiates a new two-element array object of type Object . (A reference to any object of any class or interface type can be stored in anarray element of type Object .)
  • Populates the array object with references to objects of the classes:
    • Prob05MyClassA
    • Prob05MyClassB

The same random value is passed to the constructor for both objects when they are instantiated.

Put the driver class on temporary hold

At this point, I am going to put the driver class named Prob05 on temporary hold and explain the class named Prob05MyClassA .

The interface named Prob05X

Having glanced ahead, I know that the class named Prob05MyClassA implements the interface named Prob05X so I will explain that interface first.

The interface named Prob05X is shown in its entirety in Listing 2 .

Listing 2 . The interface named Prob05X.
interface Prob05X{ public int getModifiedData();public int getData(); }//end interface

An interface definition

An interface definition can contain only two kinds of members:

  • Constants
  • Method declarations

By now, you should have studied interfaces in my online tutorials. Therefore, this explanation will be very brief.

Method declarations

Listing 2 contains two method declarations.

A method declaration does not have a body. Its purpose is to establish the programming interface for that method in any class that implements the interface (return type, name, arguments, etc.) .

A method declaration provides no information about the behavior of the method.

A method declaration in an interface is implicitly abstract.

A concrete definition is required

Any class that implements an interface:

  • Must provide a concrete version of every method that is declared in the interface, or
  • The class must be declared abstract . (In this case, abstract essentially means incomplete.)

The class named Prob05MyClassA

The class named Prob05MyClassA , which implements the interface named Prob05X , must provide concrete versions of the methods named:

  • public int getModifiedData()
  • public int getData()

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask