<< 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()

Questions & Answers

Explain the following terms . (1) Abiotic factors in an ecosystem
Nomai Reply
Abiotic factors are non living components of ecosystem.These include physical and chemical elements like temperature,light,water,soil,air quality and oxygen etc
Qasim
what is biology
daniel Reply
what is diffusion
Emmanuel Reply
passive process of transport of low-molecular weight material according to its concentration gradient
AI-Robot
what is production?
Catherine
Pathogens and diseases
how did the oxygen help a human being
Achol Reply
how did the nutrition help the plants
Achol Reply
Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is respiration
Deborah
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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