Lecture 1 Summary - nus-cs2030/2122-s1 GitHub Wiki
The first two OO principles
Abstraction
- Implementor defines the abstraction using lower-level data and processes.
- Client uses the high-level data-type and methods.
Data Abstraction
Let information be intelligent/meaningful.
Functional Abstraction
If you can extract out a meaningful function within another function, do it.
Encapsulation
- Package related data and behaviour in a self-contained unit.
- Hide information/data from the client and allow access only through methods provided.
Packaging
Code of different functionalities should reside in different classes/places.
Information Hiding
The use of
private
access modifiers. This prevents client access to lower-level details of the implementer.
Good OOP design
Tell-Don't-Ask!
Tell an object what to do, don't ask an object for data and act on it.
Immutability of objects
Void methods that mutate states should be avoided. Declaring all instance fields as
private final
helps to ensure both encapsulation and immutability.
Bottom-up testing to avoid cyclic dependencies
Avoid two classes requiring each other to be tested.
- Hard dependencies: Instance field/variable references.
- Soft dependencies: Method references.
Java Memory Model
Stack
- LIFO for storing method call records
Heap
- Storing all the
new
invocations - Garbage collection
Non-heap
- Metascape, to be discussed later
Illustrations can be found in the lecture slides.