midterm review

Static Classes and Members

  • Static classes in Java cannot access instance variables of their outer class.

  • Important Point: A static nested class cannot access non-static members. For example:

    • An A List Node cannot access the A List size.

  • Static classes only access static members.

Interfaces and Inheritance

  • Interfaces cannot be static in Java as they are not tied to an instance of a class.

  • An interface defines a contract for classes that implement it:

    • Classes extending interfaces gain all functions declared in the interface.

  • Use implements for classes that implement an interface and extends when an interface inherits from another interface.

Compilation and Interpretation Process

  • Java goes through a two-step process:

    1. Compilation: Converts .java files into .class files.

    2. Interpretation: Executes the compiled class files.

  • IntelliJ handles compilation automatically but provides warnings when compilation fails.

    • It checks types before actual compilation, ensuring that the passed values to methods make sense.

  • Compile-time errors are common, especially when a method or variable is not defined or type mismatch occurs.

  • Runtime errors are less common in Java compared to languages like Python.

Abstract Data Types (ADT)

  • An ADT is defined by its operations rather than implementation details:

    • For example, a deck ADT includes operations like first(), last(), removeFirst(), removeLast(), etc.

  • ADTs usually do not maintain state, lacking any defined instance variables.

Comparable vs. Comparator Interfaces

  • Comparable: Used when there is a natural way to compare objects of the same type.

  • Comparator: Used when multiple comparison methods are required.

Accessing Class Members

  • Use ClassName.variable to access static members and this.variable to access instance variables.

  • A class must implement all methods from any interface it implements or else it results in a compile-time error.

Understanding Class Relationships

  • Implements: Transitions from abstract definitions (interfaces) to concrete implementations (classes).

  • Extends: Indicates that one interface or class inherits properties from another.

  • For instance, deck extends iterable means all decks have to provide iterable methods.

  • Always ensure classes that extend an interface implement its required methods.

  • Classes can inherit methods from superclass interfaces as long as they adhere to the prescribed methods.

robot