Java Tutorial/Class Definition/Interface
Содержание
- 1 A Partial Interface Implementation
- 2 Encapsulating Constants in a Program
- 3 Extending Interfaces
- 4 Fields and Methods in an Interface
- 5 Initializing interface fields with non-constant initializers
- 6 Interfaces and Abstract Classes
- 7 Interfaces and Multiple Inheritance
- 8 Interfaces and Polymorphism: Using Multiple Interfaces
- 9 Multiple interfaces
- 10 Nesting Classes in an Interface Definition
- 11 To implement an interface: use the implements keyword after the class declaration
A Partial Interface Implementation
<source lang="java">
interface Conversions {
double INCH_TO_MM = 25.4; double OUNCE_TO_GRAM = 28.349523125; double POUND_TO_GRAM = 453.5924; double HP_TO_WATT = 745.7; double WATT_TO_HP = 1.0 / HP_TO_WATT; public double inchesToMillimeters(double inches); public double ouncesToGrams(double ounces);
}</source>
Encapsulating Constants in a Program
<source lang="java">
interface ConversionFactors {
double INCH_TO_MM = 25.4; double OUNCE_TO_GRAM = 28.349523125; double POUND_TO_GRAM = 453.5924; double HP_TO_WATT = 745.7; double WATT_TO_HP = 1.0 / HP_TO_WATT;
}</source>
25.4
Extending Interfaces
<source lang="java">
interface ConversionFactors {
double INCH_TO_MM = 25.4;
} interface Conversions extends ConversionFactors {
double inchesToMillimeters(double inches);
}</source>
Fields and Methods in an Interface
- Fields in an interface must be initialized and are implicitly public, static, and final.
- You declare methods in an interface just as you would in a class.
- Methods in an interface do not have a body.
- All methods are implicitly public and abstract
<source lang="java">
interface Conversions{
double inchesToMillimeters(double inches);
}</source>
Initializing interface fields with non-constant initializers
<source lang="java">
import java.util.Random; public class MainClass {
public static void main(String[] args) { System.out.println(RandVals.randomInt); System.out.println(RandVals.randomLong); System.out.println(RandVals.randomFloat); System.out.println(RandVals.randomDouble); }
}
interface RandVals {
Random rand = new Random(); int randomInt = rand.nextInt(10); long randomLong = rand.nextLong() * 10; float randomFloat = rand.nextLong() * 10; double randomDouble = rand.nextDouble() * 10;
}</source>
3 -6942612165866507216 -8.1946244E18 5.045338828500432
Interfaces and Abstract Classes
- The interface should be regarded as a contract between a service provider and its clients.
- An abstract class is a class that cannot be instantiated
- An abstract class must be implemented by a subclass.
- In Java, the interface is a type.
Follow this format to write an interface:
<source lang="java">
accessModifier interface interfaceName { } public interface Printable {
void print (Object o);
}</source>
- The Printable interface has a method, print.
- print is public even though there is no public keyword.
Interfaces and Multiple Inheritance
<source lang="java">
interface HisInterface { } interface HerInterface { } public interface MyInterface extends HisInterface, HerInterface { }</source>
Interfaces and Polymorphism: Using Multiple Interfaces
<source lang="java">
interface ThisInterface {
public void thisMethod();
} interface ThatInterface {
public void thatMethod();
} class MyClass implements ThisInterface, ThatInterface {
// Class definition including methods from both interfaces... public void thisMethod() { System.out.println("this"); } public void thatMethod() { System.out.println("that"); }
} public class MainClass {
public static void main(String[] a) { MyClass cls = new MyClass(); cls.thisMethod(); cls.thatMethod(); }
}</source>
this that
Multiple interfaces
<source lang="java">
interface A {
void aMethod();
} interface B {
void bMethod();
} interface C {
void cMethod();
} class ClassA {
public void aMethod() { }
} class D extends ClassA implements A, B, C {
public void bMethod() { } public void cMethod() { }
} public class MainClass {
public static void t(A x) { x.aMethod(); } public static void u(B x) { x.bMethod(); } public static void v(C x) { x.cMethod(); } public static void w(ClassA x) { x.aMethod(); } public static void main(String[] args) { D h = new D(); t(h); u(h); v(h); w(h); }
}</source>
Nesting Classes in an Interface Definition
An inner class to an interface will be static and public by default.
<source lang="java">
interface Port {
// Methods & Constants declared in the interface... class Info { // Definition of the class... }
} public class MainClass {
public static void main(String[] a) { Port.Info info = new Port.Info(); }
}</source>
To implement an interface: use the implements keyword after the class declaration
<source lang="java">
public class CanonDriver implements Printable {
public void print (Object obj) { // code that does the printing }
}</source>
- An implementation class has to override all methods in the interface.
- A class can implement multiple interfaces.