Interface Members


Interface Members

The body of an interface will usually consist of abstract method and/or constant declarations. These elements are called the interface members. An interface can also declare classes and interfaces as its members although this is not commonly the case. Variables declared inside interfaces are implicitly public , static , and final ; i.e., they are constants. The naming convention for interface constants is that the names are in all capital letters . When a class implements an interface that declares constants, it has direct access to them. You do not need to write the interface name to access them.

Methods declared within interfaces are implicitly abstract . Method declarations consist of the method return type, method name, input parameter list, and a semicolon. All interface members are implicitly public and cannot be modified with the transient , volatile , or synchronized keywords.

Example: Defining Interface Members

A body that carries an electric charge will generate an electric field. The Electrostatic interface is designed to be implemented by classes that represent charged bodies. The interface declares a method named getElectricField() that returns the electric field of the body. The interface also defines a constant named EPS0 that represents the permittivity of free space. This constant is used in electric field calculations.

 public interface Electrostatic {   //  permittivity of free space, C^2/(N-m^2)   double EPS0 = 8.85e-12;   double getElectricField(); } 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net