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
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);
}
Encapsulating Constants in a Program
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;
}
25.4
Extending Interfaces
interface ConversionFactors {
double INCH_TO_MM = 25.4;
}
interface Conversions extends ConversionFactors {
double inchesToMillimeters(double inches);
}
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
interface Conversions{
double inchesToMillimeters(double inches);
}
Initializing interface fields with non-constant initializers
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;
}
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:
accessModifier interface interfaceName {
}
public interface Printable {
void print (Object o);
}
- The Printable interface has a method, print.
- print is public even though there is no public keyword.
Interfaces and Multiple Inheritance
interface HisInterface {
}
interface HerInterface {
}
public interface MyInterface extends HisInterface, HerInterface {
}
Interfaces and Polymorphism: Using Multiple Interfaces
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();
}
}
this that
Multiple interfaces
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);
}
}
Nesting Classes in an Interface Definition
An inner class to an interface will be static and public by default.
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();
}
}
To implement an interface: use the implements keyword after the class declaration
public class CanonDriver implements Printable {
public void print (Object obj) {
// code that does the printing
}
}
- An implementation class has to override all methods in the interface.
- A class can implement multiple interfaces.