Week 3 Resit Flashcards
Procedural Abstraction
concept in cs where details are hidden within a procedure or function, allowing users to focus on high-level operations
Can be done within:
- An expressionm
- A statement
How does Procedural Abstraction can be done ?
Can be done by means of methods that:
- Operate on parameter objects
- Return a primitive value, object, or void
What is Modularisation for data
Combining variables of primitive types that frequently occur together into a single abstraction, instead of handling them separately
How can Modularisation for data can be done?
Grouping can be accomplished by String, array, and class, but only class can introduce a new name for the type
Top-down Design
- Given one big problem, split it into smaller subproblems
- Continue until reaching trivially solvable problems -> solutions
Bottom-up Design
- Given many small solutions, combine them into bigger solutions
- Continue until reaching a solution of the main problem
Enumerations
- Special data type that consists of a set of named constants (enumerators)
- Objects of an enumeration can take any of the values of the enumerators
Operations for Enumeration T
- T.VALUE (where VALUE is possible value)
- T.values()
- v.name() (v is an object of type T)
- v.toString()
-T.valueOf(s) - returns the enumeration constant for name s - v.ordinal(s) - returns the position of an enumeration constant s within its enum declaration
-v.compareTo(w) - negative return value - the first (v) comes before second (w)
Records
Provide a concise way to declare unchangeable data-carrying classes by automatically generating standard methods such as constructors, getters, equals(), hashCode(), and toString() based on the class’s fields
Featues of Records
- Cannot change the values in a record
- Can only read
- Comes with many predefined method getterMethod()
Abstract Data Type
A high-level description of a set of operations that can be performed on a certain type of data, along with the constraints or properties that these operations must adhere to.
- It defines what the data type does, not how it does it
- ADTs hide the representation of data and implementation of operations
- They are usually abstract classes/interfaces that are implemented
Abstract Data Classes
- A method can be declared abstract, and this means that it does not have an implementation
- Any class containing abstract methods must be declared abstract
- An abstract class cannot be instantiated
- A subclass can define additional instance variables and implementations for inherited abstract methods by overriding
Public invariants in ADTs
The properties or conditions that must be true for the data type’s public methods to function correctly (kinda like robustness)
Interfaces
Interfaces define a collection of method headers with contracts
Can define constants with public static final
Can have default method implementations but still no instance variables
Somewhat like a class with abstract methods but a class can implement one or more interfaces via implements by implementing each of the methods in the interface(s)
Interfaces can be viewed as a type; its values are the values of all classes that implement the interface
Interfaces vs. Abstract Classes
- Abstract classes support single inheritance while interfaces support multiple
- Interfaces cannot have constructors while AC can
- All fields in interfaces are implicitly public, static, and final
- Interfaces cannot have instance variables
ADTs as specification
abstract classes and interfaces can both be used to hold just the specification of a class, leaving the actual implementation to the classes that extend/implement them
ADT implementation Components for values
- Data representation - defined in terms of instance variables that represent intended abstract values of the ADT
- Representation invariant - refers to the conditions to be satisfied by the instance variables in order to make sense as a representation of an abstract value
- Abstraction function - maps each representation that satisfies the rep invariant to the represented abstract value
ADT implementation Components for Operations
- Involves providing a method implementation adhering to the contract for each operation
- The pre- and postconditions of each operation must be re-interpreted in terms of the data representation, using the abstraction function
- Also, the public invariant must be honoured
Polymorphisms
The ability of objects of different classes to be treated as objects of a common base class
Primitive vs. class type variables
- A variable of a primitive type always holds a value of that exact primitive type
- A variable of a class type T can hold a null reference or a reference to an instance of class T or of any class that is a subclass of T
Liskov Substitution Principle (LSP)
Let U be a subclass of T.
Type U is called a subtype of type T when:
- In each place where an object of type T can be used, you can substitute an object of type U without affecting the correctness of the program
A subtype is not only syntactically a substitute (it compiles), but also semantically (it works).