Unit 9: Inheritance
Interfaces in Java
Introduction to Interfaces
Java interfaces are core components of the Java programming language that enable abstraction and support the capabilities of multiple inheritance without the complications of multiple inheritance's implementation. Interfaces act as contracts for implementing classes, setting forth a certain set of methods that the implementing classes must define.
Delving into Interfaces
What are Interfaces?
- Abstraction Tool: Interfaces provide a way to achieve full abstraction in Java, as they can have only abstract methods (from Java 9, they can also contain default and static methods).
- Multiple Inheritance: Java does not support multiple inheritance through classes, but it does through interfaces. A class can implement multiple interfaces.
Declaring and Implementing an Interface
An interface is declared using the interface keyword:
public interface Movable {
void move();
}
A class implements an interface using the implements keyword:
public class Car implements Movable {
@Override
public void move() {
System.out.println("Car is moving!");
}
}
Default and Static Methods in Interfaces (From Java 9)
Java 9 introduced the ability for interfaces to contain default and static methods:
- Default Methods: Allows interfaces to have methods with implementations without affecting the classes that already use this interface.
- Static Methods: Similar to static methods in classes, but they cannot be overridden in implementing classes.
public interface Movable {
void move();
default void start() {
System.out.println("Starting...");
}
static void stop() {
System.out.println("Stopped.");
}
}
Did You Know?
Default methods were introduced to provide backward compatibility for old interfaces so that old interfaces can use and provide the implementation of new methods without affecting classes that already use this interface.
The Power of Interfaces
Interfaces in Java offer numerous advantages:
- They allow for the establishment of a standard that different classes can adhere to.
- They promote code reusability.
- They facilitate the concept of "programming to an interface, not an implementation."
Important Note
Interfaces cannot contain instance variables. Any variable declared in an interface is implicitly public, static, and final.
Summary
Java interfaces play a pivotal role in promoting abstraction, facilitating multiple inheritance, and ensuring adherence to specific contracts or standards in software design. Mastering the intricacies of interfaces is crucial for any student aiming to excel in AP Computer Science A and beyond.
References
AP CSA Homework Assignment
Assignment: Building a Shape Interface
Instructions
- Create a Java interface named Shape.
- Add methods double area() and double perimeter() to the interface.
- Implement the Shape interface in two classes: Circle and Rectangle. For the circle, use a field radius, and for the rectangle, use fields length and width.
- Provide the implementations for the area() and perimeter() methods in both classes.
- In a main method, create instances of both shapes, compute their areas and perimeters, and display the results.