Skip to content
bingmeow1999 edited this page Nov 30, 2020 · 7 revisions

Abstraction

  • Data abstraction

Let information be intelligent/meaningful.

  • Function abstraction

If you can extract out a meaningful function within another function, do it.

Encapsulation

  • Packaging

Code of different functionalities should reside in different classes/places.

  • Information hiding

Private access.

Principle of Good OOP Design

  • Tell-Don't-Ask

Tell an object what to do, rather than asking an object for data and acting on it.

  • Immutability

Void methods that mutate states should be avoided. Declaring fields as final helps to ensure immutability

Java Memory Model

A quick dive into Java's memory model. Something that we learnt in CS2030 is to understand that Primitive types get stored in the heap and references to local variable gets stored in the stack. A quick illustration:

  • Stack
    • LIFO (Last In First Out) stack for storing activation records (stack frame) of method calls
    • Local primitive type variables are stored in the stack
    • References to local variables are stored in the stack, whereas their respective Objects are stored on the heap
    • Note: The method local variables not stored here until the method is being called OR during the program run
  • Heap
    • Stores Java Objects upon invoking new
    • Garbage collection when the variables are no longer used or referenced is done here (freeing up memory space by removing unused variables)
    • Stack items (e.g. method variables) refer to objects on the heap
    • Primitive reference types are stored here as well (e.g. Double, Integer, Character, etc.)
  • Non-heap/Metaspace
    • Stores loaded classes & other metadata

There was also discussion about it on issue : https://github.com/nus-cs2030/2021-s1/issues/527#issue-752621947

Clone this wiki locally