Sie sind auf Seite 1von 3

What Is an Interface?

As you've already learned, objects define their interaction with the outside world through the methods that they expose. Methods form the object's interface with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to turn the television on and off. In its most common form, an interface is a group of related methods with empty bodies. A bicycle's behavior, if specified as an interface, might appear as follows:
interface Bicycle { void changeCadence(int newValue); void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); } // wheel revolutions per minute

To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as ACMEBicycle), and you'd use theimplements keyword in the class declaration:
class ACMEBicycle implements Bicycle { // remainder of this class implemented as before }

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
Operator Precedence Operators postfix unary multiplicative additive shift relational equality
expr++ expr-++expr --expr +expr -expr ~ ! * / % + << >> >>> < > <= >= instanceof == !=

Precedence

bitwise AND

&

bitwise exclusive OR ^ bitwise inclusive OR | logical AND logical OR ternary assignment


&& || ? : = += -= *= /= %= &= ^= |= <<= >>= >>>=

class EnhancedForDemo { public static void main(String[] args){ int[] numbers = {1,2,3,4,5,6,7,8,9,10}; for (int item : numbers) { System.out.println("Count is: " + item); } } }

In this example, the variable item holds the current value from the numbers array. The output from this program is the same as before:
Count Count Count Count Count Count Count Count Count Count is: is: is: is: is: is: is: is: is: is: 1 2 3 4 5 6 7 8 9 10

class OuterClass { ... static class StaticNestedClass { ... } class InnerClass { ... } }

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.)
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

OuterClass.InnerClass innerObject = outerObject.new InnerClass(); class ChessAlgorithm { enum ChessPlayer { WHITE, BLACK } ... final ChessPlayer getFirstPlayer() { return ChessPlayer.WHITE; } ... }

Methods called from constructors should generally be declared final. If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results. Note that you can also declare an entire class final this prevents the class from being subclassed. This is particularly useful, for example, when creating an immutable class like the String class.

Das könnte Ihnen auch gefallen