Show Menu
Cheatography

Java Class Design (OCA) Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Inheri­tance

Why inheri­tance?
1. DRY(Don't Repeat Youself): No copy and paste, use has a (compo­sit­ion), is a ( inheri­tance)
2. Extens­ible: easy to add/modify business logic and share the code)
Java inheri­tance
Single inheri­tance, one parent only , all instance variables and methods inheri­tated
one parent could have multiple child classes
parent­/su­per­/ge­neric <- child/­sub­/sp­ecifc

Chaining constr­uctor

Chaining constr­uctor
child constr­uctor call parent constr­uctor
reasons 1: private parent instance variable
reasons 2:clean and neat:c­ompare: loosen restrict, add using setter(how about set is not logically OK)
super() is always called explic­itl­y()or implicitly in the first line of child constr­uctor. -> if a class will be extended, it must has no argument constr­uctor, or do not have any constr­uctor.
super.x­xx(dot operation on super) won't follow first line rule of constr­uctor.
More on protected
Package private + subclass
parent­/child + different packages, access protected state/­beh­avior from parent
1) direct call 2)obj ref of the child itself 3) obj ref of parent or other sub class can NOT.

Overriding

in child change behavior of parent
1. same signature as parent
2. return type, same or subtype, primitive exact the same(no promoting and wrap)
3. access­ible: same or wider
4. exception: same or fewer/­sub­typ­e/r­untime
privat­e/s­tatic methods are hidden, not overriden
polymo­rphism applies only instance method
super. ( dot notation) to access parent state or behavior
never hiding static member ( variab­le/­method) or instance variable, bad practice, confusing.

Covariant returns

overriding method return a same or subtype of parent returned
exact the same for primitive return

Three Faces of Final

final variable
Constant
enum constants implicitly static final
constant used in switch
final method
no overriding
final class
no extends, java.l­ang.String
Switch: litera­l,c­ons­tan­t,enum, compiler time bind, variable or method return not, due to not known how many cases should be listed.

Class/­Object invocation order

1 static var=de­fault
child-­>base
2 static{}, explicit value assign to static var
base, in statement order
repeat 1,2 in child hierarchy order
->child
4 instance var=de­fault
child-­>base
5 {},exp­licit value assign to instance var
base, in statement order
6 constr­uctor
base
repeat 4,5,6 in child hierarchy order
->child
static initia­liz­ation 1-3 execute only once when first class is loaded.

Overlo­ading

same name+d­iff­erent parameters
Signat­ure­=na­me+­par­am,­unique in a class
Others­(return type,m­odi­fiers, except­ions) not matter
overlo­ading matching order:
exact match ->p­rom­oti­ng-­>wr­apper =>v­arargs (exact match,­pro­mot­ing­,wr­apper)

Private methods redecl­aration

not inheri­tated
can redecclare a method with same signature
 

hiding static method

with static parent method, could not override
hiding - no polymo­rphism
4 overriding rules + static modifier
Never hiding static methods in practice, confusing and bad habit

Inherite variables

never overri­ding, always hiding if same name
when hiding a variables, using super and this to access parent and child
static and non static follow the same rule for hiding
private variables inherited but could not access directly.
never hiding variables in practice, confusing and hard to read code

Abstract classes

Why?
genera­liz­ation, inheri­tance, overriding and polymo­rphism
simply code, beauty, no DRY
prevent improper instan­tiate of parent classes
Abstract class rules
>=0 abstract methods
can't initia­lized
public /package private only, must be extends, so private or final is not allowed, protected is not logic/­mea­ningful
extends abstract class means overriden all abstracted methods or declared as abstract
first concrete class must have implem­ented all abstract method directly or indirectly
Abstract methods
in abstract class
can not be private, final, static (must be overriden)
no body, even {}
overriding rules(4) must be followed: same signature, broader or same visibi­lity, narrower or same return type, narrower or same exception throws/or runtime exception
 

Interface

public abstract interface ....{}
public static final MIN_DE­PTH=3
init at the statement
interface extends interf­ace­1,i­nte­rfa­ce2,...
multiple extends allowed here
class inpluments interf­ace­1,i­nte­rfa­ce2,...
multiple implements
can redecclare a method with same signature
Rule for interface
can not instan­tiated
may have no methods at all
public / default only
not privat­e,f­inal, protected for interface
all methods must be public
not privat­e,f­inal, protected for methods
abstract method by default
in java8, default, static methods with a body allowed
default interface methods
java 8
mainly for backward compat­­ib­ility
public default double calc(){}
only in interface
can be redeclared as abstract or overriden with a different body
not static, final,or abstract ( overriden)
not privat­­e,­p­r­ot­­ected
Multiple inheri­tance problem
default method in interface, if not overriden, will cause compiler error if default methods with same signuature existed
for interfaces without default methods, there is no this issue
if default method is overriden, also no ambiguity problem.
Static method
java 8 above
public or default only
with a body
call with interface name, not with object ref
this avoid ambiguity cause be multiple inheri­tance
static methods in interface could be declared as default in sub interface
designed to offer utility functions

Polymo­rphism

heart of inheri­tance ( overri­din­g+p­oly­mor­phism)
separate of concern, flexib­le/­ext­ensible coding
properties of an object to take on many different forms,­com­piling time- ref by super class/­int­erface ref, at run time multiple behavi­or,­based on the object itself
multiple refere­nces(on the stack) ( ref of type of super class, interf­ace), static binding
multiple object (on the heap )behav­iors, dynamic binding
Virtual methods
dynamic method dispat­ching
overriden methods
non privat­e,s­tat­ic,­final
a method in which the specific implem­ent­ation is not determined until run time; at compiling time, parent ref is used, at run time, implem­ent­ation based on the child obj referenced
Object casting
implicit up casting ( child ->p­arent)
explicity down casting( parent­->c­hild)
error for non-pa­ren­t/child object casting
safe casting: obj1.i­nst­anc­eOf­(obj2)
Polymo­rphic parameters
parameter is parent class or interface type
passing the child obj or obj implem­ented the interface
auto up casting
a reference variable may only send messages that are available to its type.being available to an object != being declared inside an object

when Parent­/Child not belong to same package

in extending class
protected parent members could be access directly. if by refere­nces, only by the ref of extending class itself
ref variables of other child class or event he parent class COULD NOT access parent members inside extending calss
constr­uctor()
if constr­uctor missing access modifier ( package private), the child class could not instan­tiated.
if class to be extends, constr­uctor must be public or protected, if in different package.

Pass by value vs by reference

Both are passing by copy
the original content ( primitive value or object memory address) variables not affected
if passing a copy of obj address, changes to the object on the heap will shared with all refere­nces.
reassign the reference in callee will not affect the ref in caller
variable on stack frame, obj on heap