Implementing an Interface


A class indicates that it will implement an interface by including the interface name after the implements keyword in the class declaration. If the class will implement more than one interface, the interface names are separated by commas.

 [modifiers] class class_name                  implements interface1, interface2, ... 

To implement an interface, a class must provide an implementation of all the methods declared by the interface. If a class does not provide implementation for all of the interface methods , the class must be declared abstract and you cannot create an instance of this class.

A class that implements an interface has direct access to all constants defined in the interface. The constants can be accessed using only their simple name ( EPS0 rather than Electrostatic.EPS0 ). A class that does not implement the interface can still access the constants defined in the interface by using the qualified name ( Electrostatic.EPS0 ).

Example: Implementing an Interface

Let us define two classes that will implement the Electrostatic interface described in the previous section. The InfiniteChargedPlate class represents an infinite charged plate. The electric field normal to an infinite flat plate is given by this expression ”

Equation 10.1

graphics/10equ01.gif


The electric field, E, in Eq. (10.1) is the force per unit charge and has units of N / C . The parameter s is the surface charge density in C / m 2 . The constant e is the permittivity of free space. Because the InfiniteChargedPlate class implements the Electrostatic interface, it must implement getElectricField() . The class can implement this method any way it likes and does so according to Eq. (10.1).

 public class InfiniteChargedPlate                           implements Electrostatic {   private double chargeDensity;   public InfiniteChargedPlate(double chargeDensity) {      this.chargeDensity = chargeDensity;   }   //  Implement the Electrostatic interface method   public double getElectricField() {      return chargeDensity/(2.0*EPS0);   } } 

The second class is named ParallelPlate and it represents two infinite, parallel, oppositely charged plates. Outside the plates, the electric fields cancel each other and the net field is zero. Between the plates the fields combine and the total electric field is given by the expression

Equation 10.2

graphics/10equ02.gif


The ParallelPlate class must implement getElectricField() and does so according to Eq. (10.2). Note that both classes have direct access to the EPS0 variable defined in the Electrostatic interface. There is no need to type Electrostatic.EPS0 .

 public class ParallelPlate implements Electrostatic {   private double chargeDensity;   public ParallelPlate(double chargeDensity) {     this.chargeDensity = chargeDensity;   }   //  Implement the Electrostatic interface method   public double getElectricField() {     return chargeDensity/EPS0;   } } 

Finally, we will write a simple driver program that will create ParallelPlate and InfiniteChargedPlate objects and call the getElectricField() methods.

 public class ImplementDemo {   public static void main(String args[]) {     // Create InfiniteChargedPlate and ParallelPlate     // objects     InfiniteChargedPlate plate =                       new InfiniteChargedPlate(1.0e-6);     ParallelPlate plate2 = new ParallelPlate(1.0e-6);     //  Write out their electric field intensities     System.out.println("infinite plate field is " +                           plate.getElectricField());     System.out.println("parallel plate field is " +                           plate2.getElectricField());   } } 

Output ”

 infinite plate field is 56497.175 parallel plate field is 112994.350 


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