CS 211 week 6 abstraction

Cards (23)

  • A child class must fulfill the "contract" by overriding all the abstract methods
  • Abstract Classes

    1. Abstract class provides a top-down representation indicating placeholders
    2. Placeholders are required to be reified in subclasses
  • Abstract Classes
    1. Abstract class's subclass must override the abstract methods of the parent, or it too will need to be abstract
    2. An abstract method cannot be defined as final or static
  • Method Invocation
    Syntax (explicit) vs Semantics (implicit)
  • Abstract class represents buyable items with a barcode and associated methods, with specific implementations for each item
  • When a method is called, the parameters in the invocation are copied into the arguments in the method header
  • Abstract Classes

    1. Abstract class is a template in a class hierarchy that represents a general concept
    2. Abstract class cannot be instantiated
    3. Abstract classes are meant to be extended
  • When a class is abstract, the contract of "make an object for me and I'll provide these methods/variables for you" is unfulfilled, so we can't make objects of the abstract class
  • Abstract classes are an important element of software design allowing the establishment of common elements in a hierarchy that are too generic to instantiate
  • Abstract Classes
    1. Abstract classes may contain zero or more abstract methods
    2. Abstract methods have no body and need to be explicitly marked abstract
    3. Non-abstract methods are also allowed in an abstract class with bodies
  • Abstract class
    When a class is abstract, the contract of "make an object for me and I'll provide these methods/variables for you" is unfulfilled, so we can't make objects of the abstract class
  • Using interfaces
    Classes can implement interfaces to define specific behaviors. Once implemented, the interface methods can be used in the class
  • When is inheritance more appropriate?
  • Abstract class

    A child class must fulfill the "contract" by overriding all the abstract methods, then all methods are available, and we can instantiate the child class. Sometimes a child class is also abstract, leaving the contract to later generations
  • When we learned about inheritance, we stated that there can only be one parent class: no "multiple inheritance" allowed
  • java.lang.Comparable
    • One method: return negative for "less than", zero for "equal", positive for "greater than". Provides a consistent way to sort data
  • Implement your Noisy interface for any class of your choosing

    Implement the Noisy interface in a class
  • java.lang.Iterator

    • Three methods: compareTo(Object other), hasNext(), next(), remove()
  • Abstract classes vs Interfaces
  • Main method
    1. Create objects from classes that implement an interface and store them in variables a and b
    2. Create a variable of the interface type called i
    3. Store a or b into i
    4. Store i into a or b
    5. Use casting
    6. Write a method accepting a parameter of the interface type
  • Practice Problems
    1. Create an enumerator for the sign of a number (positive, negative, zero)
    2. Create a variable using the Sign enumeration and use it in a switch to print if the number is "bigger than", "equal to", or "less than" zero<|Create an enumeration, Quadrant, representing the four quadrants of the two-dimensional plane<|Update Quadrant enumeration to have two private fields for the x and y Signs, add constructor calls to use these fields for Quadrant values, write a toString method for Quadrant enumeration, write getXSign and getYSign methods to work on a Quadrant value
  • Advanced Enumerators
    1. Adding fields
    2. Adding (non-public) constructors
  • Java

    • Uses the class mechanism behind the scenes