However, the car must be able to perform certain operations: Go forward Slowdown/stop light) Go in reverse Turn left (signal light) Turn right (signal light) Etc. operations are defined by an interface (contract) that defines what a car is from the perspective of how it is used. The interface does not concern itself with how the car is implemented. That is left up to the car manufacturers. 6.34. Defining a Java Interface Use the keyword to define an interface in Java. interface The naming convention for Java interfaces is the same as for classes: CamelCase with an initial capital letter. The interface definition consists of public abstract method declarations. For example: public interface { double getArea(); double getPerimeter(); } All methods declared by an interface are implicitly methods. public abstract You can omit either or both of the and keywords. public static You must include the semicolon terminator after the method declaration. Rarely, a Java interface might also declare and initialize fields for use by public static final subclasses that implement the interface. Any such field must be declared and initialized by the interface. You can omit any or all of the and keywords in the declaration. public, static, 6.35. Implementing a Java Interface You define a class that implements a Java interface using the keyword implements followed by the interface name. For example: class Circle implements Shape { // ... } A concrete class must then provide implementations for all methods declared by the interface. Omitting any method declared by the interface, or not following the same method signatures and return results in a compilation error. An abstract can omit implementing some or all of the methods required by an interface. In that case concrete subclasses of that base class must implement the methods. A Java class can implement as many interfaces as needed. Simply provide a comma-separated list of interface names following the keyword. For example: implements class ColorCircle implements Shape, Color { // ... } A Java class can extend a base class and implement one or more interfaces. In the declaration, provide the keyword and the base name, extends followed by the keywords and the interface name(s). For example: implements class Car extends Vehicle implements Possession { // ... } public class Circle implements Shape {
However, the car must be able to perform certain operations: Go forward Slowdown/stop light) Go in reverse Turn left (signal light) Turn right (signal light) Etc. operations are defined by an interface (contract) that defines what a car is from the perspective of how it is used. The interface does not concern itself with how the car is implemented. That is left up to the car manufacturers. 6.34. Defining a Java Interface Use the interface keyword to define an interface in Java. The naming convention for Java interfaces is the same as for classes: CamelCase with an initial capital letter. The interface definition consists of public abstract method declarations. For example: public interface { double getArea(); double getPerimeter(); } All methods declared by an interface are implicitly public abstract methods. You can omit either or both of the public and static keywords. You must include the semicolon terminator after the method declaration. Rarely, a Java interface might also declare and initialize public static final fields for use by subclasses that implement the interface. Any such field must be declared and initialized by the interface. You can omit any or all of the public, static, and keywords in the declaration. 6.35. Implementing a Java Interface You define a class that implements a Java interface using the implements keyword followed by the interface name. For example: class Circle implements Shape { // ... } A concrete class must then provide implementations for all methods declared by the interface. Omitting any method declared by the interface, or not following the same method signatures and return results in a compilation error. An abstract can omit implementing some or all of the methods required by an interface. In that case concrete subclasses of that base class must implement the methods. A Java class can implement as many interfaces as needed. Simply provide a comma-separated list of interface names following the implements keyword. For example: class ColorCircle implements Shape, Color { // ... } A Java class can extend a base class and implement one or more interfaces. In the declaration, provide the extends keyword and the base name, followed by the implements keywords and the interface name(s). For example: class Car extends Vehicle implements Possession { // ... } public class Circle implements Shape {
notes-summary-java-oop.docx