CPSC-2150 Exam 2 Study Guide

Interfaces

Key Questions

  • What is an interface?

  • Why do we use interfaces?

  • How to develop interfaces in Java?

What is an Interface?

Interfaces define interactions in software architecture, serving as a blueprint for classes that implement them. They establish a contract for what methods classes should include, without specifying how these methods should be implemented. This promotes loose coupling and improves code maintainability.

  • Interfaces define interactions in software.

  • User Interface (UI): Interface between the user and a program.

  • Characteristics of Interfaces:

    • Specification: They describe what classes should do, not how they should do it, focusing on the method signatures and return types.

    • Concept Representation: Represent coherent concepts like data structures (stacks, queues, etc.), making software design more intuitive.

    • Contractual Obligation: Specify available methods and the contracts for these methods, ensuring any implementing class fulfills the outlined responsibilities.

    • Multiple Implementations: Multiple classes can implement the same interface, allowing for varied implementations depending on specific use cases.

    • Client Abstraction: Clients utilize interfaces and interact with objects without needing to understand their implementation details, enhancing flexibility and reducing dependencies in the codebase.

    • They describe what classes should do, not how.

    • Represent coherent concepts (stacks, queues, etc.).

    • Specify available methods and the contracts for these methods.

    • Multiple classes can implement the same interface.

    • Clients use interfaces, ignoring implementation details.

Built-in Interfaces in Java

  • Example: List Data Structure

    • List Interface: A Java interface that establishes methods for managing a collection of elements.

    • List is a Java interface.

      • Provides contracts for methods like add, remove, get, set, and size.

    • ArrayList: A resizable array implementation of the List interface that provides fast access and is optimized for quick retrieval.

    • LinkedList: A doubly-linked list implementation that offers better performance for insertion and deletion operations in the middle of the list but slower access times compared to ArrayList.

From Classes to Interfaces

  • A class provides a unique solution to a problem.

  • There are many methods to solve problems (e.g., sorting: Bubble, Merge, Quick, Insertion, Selection), each with trade-offs.

Interfaces and Multiple Classes

  • Example of why multiple implementations exist: Sorting algorithms optimized for different factors (speed vs. memory efficiency).

  • Interfaces allow various implementations to solve the same problem in different ways.

  • Different implementations, such as sorting algorithms, exist to optimize for various factors, including performance speed and memory efficiency. Interfaces allow for the use of alternate implementations while ensuring they adhere to the same interface contract.

Example of Trade-Offs

  • Grid Interface:

    • A hypothetical interface that produces methods like addMarker and whatsAtPos.

    • Implementations include:

      • GridFast: Uses a 2D array—fast access but poor memory efficiency.

      • GridMem: Uses a HashMap—slower access but more memory efficient.

    • Both implementations fulfill the same interface but with different internal mechanisms.

Interfaces in Use

  • Objects implementing the same interface (e.g., GridMem and GridFast) are interchangeable.

  • Method calls like myGrid.addMarker(...) work regardless of the specific implementation.

Interface Syntax in Java

  • Defining an Interface:

    • public interface Fruit { void setWeight(double aWeight); ... }

    • A class must implement all methods defined in an interface with matching signatures, adhering strictly to the interface's contract.

Declaring an Interface

  • Interfaces look like class definitions but use the keyword interface.

    • Methods have no bodies (yet).

    • Cannot include private attributes; contains only public static final constants.

Review Question

  • Which contains private attributes?

    • The class that implements the interface.

Instantiating an Interface

  • Variables can be declared as interface types.

  • Interfaces cannot be instantiated directly.

  • Instances must be of classes implementing the interface.

Declared Type vs Dynamic Type

  • Declared Type: Set at compile time.

  • Dynamic Type: Determined at run time (using assigned statements).

    • Example: instanceof operator identifies the actual type at runtime.

Declared Types

  • Only interface members should be called by clients.

  • Class methods can access all class members.

    • Use of default methods will be explored further later.

Simple Rule #1 - Compiler

  • Interfaces can only function as declared types.

  • All dynamic types are classes.

  • Classes are instantiated, whereas interfaces are not.

Good Practice: Code to Interface

  • This involves programming with interface types rather than concrete classes.

    • Enhances flexibility of method arguments and return types.

    • Encourages change in implementation without affecting client code.

Core Concepts

Interface Specification

  • Interfaces should provide necessary information while abstracting implementation details.

  • Contracts detail what a user must know beyond just method names and parameters.

Information Hiding

  • Emphasizes hiding unnecessary details from users.

  • Interfaces facilitate information hiding, allowing users to focus solely on functionality rather than implementation.

Abstraction Importance

  • Abstraction hides implementation specifics while retaining usability.

Interface Specification Examples

  • The Grid interface defines methods without specifying implementation.

    • Example: @initialization_ensures outlines post-construction state.

Constraints and Definitions

  • @defines and @constraints contextualize variables without specific implementations.

Grid Example Interface Specification

  • Represents a 2D grid for characters:

    • Initialization: Creates a grid of blank characters with defined dimensions.

    • Constraints: Ensure rows and columns stay within defined limits, preventing illegal accesses

Implementing Specifications

  • The implementing class connects the separate interface specifications with concrete code.

  • Use of contracts tied to invariants exposes necessary implementation details without overwhelming clients.

Transitional Topics

  • Implementing Classes: Must adhere to interface contracts.

  • Costraints outline the expectations of the implementation, ensuring functional reliability.

Factory Method Example

Overview

  • Factory method encapsulates object creation to ameliorate the client’s engagement with the constructor.

  • Enforces programming to the interface.

Implementational Use

  • Client interacts only with factory methods, promoting later adaptability without pervasive code revisions.

Observer and MVC Patterns

  • Observer patterns are utilized in event-driven UI components, enhancing responsiveness and interactivity within applications.

  • MVC (Model-View-Controller) is a design pattern maintains separation between data models, view interactions, and control flow management.