1. 程式人生 > >Interface In Java Programming

Interface In Java Programming

An interface in java is a collection of abstract methods and constant identifiers. There are no defined methods and  no fields. From that definition, if a class provides method definitions for an interface’s methods, the class is said to implement the interface. The class may also define other methods. In this post, we will see how to use an interface in java.

interface in java

The Example – An Interface In Java

In this example, we will create an interface called Employee for all the employees in a company. The information read in for each employee in question consists of employee’s name and gross pay. So, we need to list the responsibilities of the interface that will help us define the methods’ specifications. In other words, the responsibilities means the services provided to users of any class that implements the Employee 

interface. So here they are:

  • To determine if an employee’s gross pay is greater than some other employee’s gross pay.
  • To convert an employee’s name and gross pay to a string suitable for output.

Here is our Employee Interface

[java]

public interface Employee{

/**
* Determines if this this employee object’s gross pay is
* is greater than a specified employee’s gross pay
*
* @param otherEmployee – the specified employee object
* whose gross pay this Employee object’s gross pay is compared to
*
* @return true – if this Employee object’s gross pay is greater
* than otherEmployee’s gross pay
*
*/

boolean makesMoreThan(Employee otherEmployee);

/**
* Returns a string representation of this Employee object
* with the name followed by a space followed by a dollar sign
* followed by the gross weekly pay, with two fractional digits.
*
* @return a String representation of this Employee object
*
*/

String toString();

} //end of Employee interface

[/java]

The Employee interface’s method specifications are all that a user of any implementing class will need to invoke those particular methods. If you were a developer of that class on the other hand, you must decide what fields to have and then define the methods.

A good example of employee categorization would be full-time and part-time. In this example, we will develop a FullTimeEmployee implementation of the Employee interface.

Let us say we have two fields for our interface in java example: name – a String reference and grossPay – which is a double. The complete method definitions are developed from the fields and method specification.

[java]

import java.util.*; // for StringTokenizer class
import java.text.DecimalFormat;

public class FullTimeEmployee implements Employee{

private String name;
private double grossPay;

/**
* Initializes the FullTimeEmployee object to have an empty
* string for the name and
* 0.00 for the gross pay
*
*/

public FullTimeEmployee(){

final String EMPTY_STRING = “”;
name = EMPTY_STRING;
grossPay = 0.00;
} //default constructor ends here

/**
* Initialzes this FullTimeEmployee object’s name and gross
* pay from a specified String object, which consists of a
* name and gross pay, with at least one blank in between
*
* @param s – the String object from which this
* FullTimeEmployee object is initialized
*/

public FullTimeEmployee(String s){

StringTokenizer tokens = new StringTokenizer(s);
name = tokens.nextToken();
grossPay = Double.parseDouble(tokens.nextToken());
} //constructor with String parameter

/**
* Determine if this FullTimeEmployee object’s gross
* pay is greater than a specified Employee object’s
* gross pay
*
* @param otherEmployee – the specified Employee
* object whose gross pay this FullTimeEmployee
* object’s gross pay is compared to
*
* @return true – if otherEmployee is a FullTimeEmployee
* object, and the calling object’s gross pay is greater
* than otherEmployee’s gross pay. Otherwise return false
*
*/

public boolean makesMoreThan(Employee otherEmployee){

if(!(otherEmployee instanceof FullTimeEmployee)){
return false;
}
FullTimeEmployee full = (FullTimeEmployee)otherEmployee;
return grossPay > full.grossPay;
} //end method makesMoreThan

/**
* Returns a string representation of this Employee object
* with the name followed by a space followed by a dollar sign
* followed by the gross weekly pay, with two fractional digits.
*
* @return a String representation of this Employee object
*
*/

public String toString(){

final String DOLLAR_SIGN = ” $”;
DecimalFormat d = new DecimalFormat(“0.00”);

return name + DOLLAR_SIGN + d.format(grossPay);
} //end toString method
} //end class FullTimeEmployee

[/java]

If you looked carefully, you might have noticed the appearance of grossPay all by itself on the left-hand side of the > operator inside the definition of makesMoreThan() method.

So which object’s grossPay field is being compared to the grossPay field of full’s object?

The grossPay field of the object that invoked the makesMoreThan method is being compared to the grossPay field of full’s object. Let us look at a quick example here:

Assume we have already created FullTimeEmployee objects referenced by employee and bestPaid. If we then write:

[java]

/**
* Examples
*/

employee.makesMoreThan(bestPaid);

/*
* End of example
*/

[/java]

Then the comparison is between the employee object’s grossPay and the bestPaid object’s grossPay. The same rule applies if a method appears without a calling object.

In General,

If an object has called a method and a member appears without an object reference in the method definition, you may actually assume that the member is part of the calling object.

One other interesting feature in the definition of makesMoreThan method is related to the problem that the Employee interface does not have a grossPay field (or any other field). That means the Java compiler would object to a statement like this:

[java]

//compile-time error here
//
return grossPay > otherEmployee.grossPay
//
//

[/java]

According to the method specification, if otherEmployee is not a FullTimeEmployee object, false is returned. You determine that using the instanceof operator.

If the object is an instance of the given class, true is returned.

Even if false has not been returned at this point, the compiler will still not allow the expression otherEmployee.grossPay. But we can overcome this last problem with a cast: the temporary conversion of an expression’s type to another type. We did it like this:

[java]

//

FullTimeEmployee full = (FullTimeEmployee)otherEmployee;

//

[/java]

Using a cast is basically like telling the compiler, look, I know what I am doing here. So, don’t complain!

You can read more about interfaces in Java by clicking here

That is it for today folks. Thank you for reading this interface in java post and I hope you found it helpful. If you have any questions on interfaces in java, please let me know through the comments, otherwise, share this post with your friends and subscribe! Thank you!