INTERFACE CLASS IMPLEMENTATION:
Rodelio P. Barcenas
NOTE: A copy of this program and document can be downloaded at
http://www.pinoytechzone.com/interface.zip
Interface Class is a JAVA object which defines the action and
accessor methods of a target object class.
To expand the discussion, let us put a scenario where a software
project is supposed to be implemented.
Steps to be undergone in developing the software;
1. The whole project will be subdivided into objects which constitute
the project.
2. After the objects has been established, the software team
develops the objects.
3. Finished objects are then interconnected with one another.
4. Software is finished.
The Problem;
The software groups will have a natural tendency to create the objects
in their own way and can add a potential problem to steps 3 and 4.
Objects should agree to how they are supposed to be interconnected with
one another. If an object will not conform to a certain interfacing
requirement, the object cannot be used or utilized.
The Solution:
1. The interface between objects should be defined.
2. Adherence to the interface should be followed.
3. Failure to follow the interface should not be allowed.
-------------------------------------------------------
THE INTERFACE CLASS;
-------------------------------------------------------
The interface class is a java object which consists of method structures
which is supposed to be implmented by an object being developed. Failure
to conform will result to non-compilation of the object being developed.
This type of class is usually developed to ensure adherence to the
interfacing requirements between objects. Example program below explains
the concept.
/*
switch.java (interface class)
programmed by: Mr. Rodrigo Silvano
arranged by : Mr. Rodelio Barcenas
switches.java
- a class file which contains the methods that
should be created by the implementing object.
If the interface methods are not implemented
all in the implementing object, a compilation
error will result.
- all method structure of the implementing object
does not contain the program lines.
- the implementing object completes the program
for each method declared in the interface class
*/
public interface switches {
public void turnOn();
public void turnOff();
public boolean isOn();
}
-------------------------------------------------------
THE IMPLEMENTING OBJECT (Object being Developed)
-------------------------------------------------------
/*
ElectricFan.java
- an object whose methods structure comes from
the interface class switch.java
- all methods found in the interface class
(switch.java) should be declared. Otherwise,
compilation errors occurs...
*/
public class electricFan implements switches {
// instance variable declaration
int status = 0;
// action methods found in the interface Switch
public void turnOn() {
status = 1;
}
public void turnOff() {
status = 0;
}
public boolean isOn() {
return ( status == 1);
}
// addtional methods can be written here
}
-------------------------------------------------------
THE TEST PROGRAM:
-------------------------------------------------------
// Program for testing the interface class
// mainSwitch.java
public class mainSwitch {
public static void main(String arg[]) {
electricFan asahi;
asahi = new electricFan();
asahi.turnOn();
System.out.println(asahi.isOn());
asahi.turnOff();
System.out.println(asahi.isOn());
}
}