Chapter 4 Interfaces

1) An interface can declare three kinds of members:

  • constants(fields)
  • methods
  • nested classes and interfaces

  All interface members are implicitly public, but, by convention, the public modifier is omitted.

  Named constants are defined as fields, but are implicityly public, static, final, and the modifiers are omiited from the field declaration.

  Interface methods are implicity public, abstract. No other method modifiers are permitted on an interface method declaration, except for annotations. They cannot have modifiers that define implementation characteristicssuch as native, synchronized, or strictfpbecause an interface does not dictate implementation, and they cannot be final because they haven't been implemented yet. Interface methods can never be static because static methods can't be abstract.

Example:

interface Verbose {
	int SILIENT = 0; //implicitly to be public final static
	int TERSE = 1;
	int NORMAL = 2;
	int VERBOSE = 3;
	void setVerbosity(int level); // implicitly to be public abstract method
	int getVerbosity();
}

class ClassT implements Verbose {
	private int verbose;
	ClassT(int verbose) {this.verbose = Verbose.SILIENT;}
	public void setVerbosity(int level){this.verbose = level;} // has to be public method
	public int getVerbosity(){return verbose;}
}

public class InterfaceTest {
	public static void main(String[] args) {
		Verbose tmp = new ClassT(Verbose.SILIENT);
		System.out.println(tmp.getVerbosity());
	}
}

Output:

0

 

2) Unlike classes, interfaces support multiple inheritance, that means, interfaces can extends more than one interfaces. Because interfaces define no implementation of methods, and provide no per-object fields, there are no issues regarding the semantics of multiple inheritance in classes.

   An extended interface inherits all the constants declared in its superinterfaces. If an interface declares a constant of the same name as an inherited constant, regardless of their types, then the new constant hides the inherited one. If an interface inherits two or more constants with the same name, then any simple reference to the constant is ambiguous and results in a compile-time error.

  A subinterface inherits all the methods declared in its superinterfaces. If a declared method in a subinterface has the same signature (name and parameter list) as an inherited method and the same, or covariant, return type, then the new declaration overrides any and all existing declarations. If an interface inherits more than one method with the same signature, or if a class implements different interfaces containing a method with the same signature, there is only one such method. The implementation of this method is ultimately defined by the class implementing the interfaces, and there is no ambiguity there. If the methods have the same signature but different return types, then one of the return types must be a subtype of all the others, otherwise a compile-time error occurs. As with overriding in class extension, the overriding method is not permitted to throw more checked exceptions than the method it overrides. If two or more method declarations are inherited, without overriding, and differ in the exceptions they throw, then the implementation of that method must satisfy all the throws clauses of those declarations.

 

3) The most common approach to solve the problem of extending more than one class is to create an object of an implementing class and forward all the methods of the interface to that object, returning any valuesthis is often called composition.

 

4) Marker interfaces do not declare any methods but simply mark a class as having some general properties. Marker interfaces are the degenerate case of a contract because they define no language-level behaviorno methods or values. All their contract is in the documentation that describes the expectations you must satisfy if your class implements that interface. Marker interfaces can have a profound impact on the behavior of the classes that implement themconsider Cloneable.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章