1/74
Looks like no tags are added yet.
paradigm
way of framing the problem
one description, many things made from it
OOP
four tenets of OOP (APIE)
abstraction (decide what solution is made of)
polymorphism (one request, many forms of answer)
inheritance (one class reuses another)
encapsulation: bundle data with its methods
abstraction
class (description)
data
how are they related
overriding
CHILD class replaced method it inherited
same name and same params chosen at run time
java picks the method belonging to the OBJECT, not to variable’s declared type
overloading
ONE class has several methods with same name
different param lists chosen by compiler
keyword extends
one class (first one) reuses another (second one)
inheritance vs composition
IS a: write extends
HAS a: a field
important because java allows one direct superclass per class
encapsulation
bundling data (variables) and its methods into a class while restricting direct access from outside the class
declare class variables as private to hide from direct external modifications
provide public getter/setter methods for controlled reading/updating of these variables
public field vs private getter and setter
cannot refuse anything
decides what it will accept
order of structure types when writing class
fields
constructors
methods
what naming convention do class names use
PascalCase (every word capitalized, including the first)
fields you do not set are…
empty
int, long, short, byte default value
0
float, double default value
0
char default value
‘\u0000’ (not digit zero)
boolean default value
false
any object type default value
null (including String)
local variable default value
none
reading it is compile error
if there is none, probably has not been initialized
accessibility modifier & what are fields and methods usually
decides what code elsewhere is allowed to see
fields private, methods public
accessibility modifier types, visible from, and usage
private: this class only, fields
package private (none): any class in package, default
protected: package and subclasses, members a child needs
public: everywhere, deliberate interface
camelCase
variables and methods
SCREAMING_SNAKE
constants
instantiation
making an object
ex. Cookie cookie = new Cookie();
Cookie = type, cookie = reference var, new Cookie() makes object
what does the variable hold
reference, not the object
label the components of: oopclasses.Cookie@1dbd16a6
oopclass.Cookie = full class name
@ = separator
1dbd16a6 = identity hash code
NOT a memory address nor pointer
what the class can do (methods): part, where, and what it does
ex. public String cookieStory()
access modifier: public, who may call it
return type: String, void if nothing comes back
name: cookieStory, camelCase, usually a verb
signature
name and parameter types
can you overload on return type
no
compiler would have nothing to tell them apart by
what does the dot operator do
reach inside an object
instance method needs an instance, which is what the dot is for
two parts in accessing / modifying a private field
getter / accessor
returns field type, no parameters
setter / mutator
returns void, takes one parameter
write getter & setter for Baker
public String getBaker() {
return baker;
}
public void setBaker(String baker) {
this.baker = baker;
}what does “this” refer to in a setter (ex. this.baker = baker;)
the object whose method is running
baker = field of THIS object & baker = parameter
what happens if you remove “this” in a setter
compiles perfectly, does nothing at all
parameter assigned to itself
constructor name and return type
same name as class, no return type

what does the keyword new do (4 steps)
new allocates memory onto the heap
every field set to default (0, false, null)
constructor body runs, replacing defaults
new returns reference to finished object
method specifications, and is a constructor a method
has return type, is inherited, can be overridden, can be called by name
constructor is none of these → no
default constructor vs no-arg constructor
generated by compiler when none written
never seen in source
written by you w/ empty param list
can contain any code necessary
how many constructors can you have
infinite, as long as param lists differ
keyword static
belongs to class, not object
static method vs instance method
can’t use this, can’t read instance fields, no object for them to belong to
can use static members freely, restriction runs one way only
constructor vs static factory method
always returns NEW object, name fixed to class
can return existing object, can have descriptive name
java is pass by…
value
what does var do (demonstrate w/ Cookie c = new Cookie();
var c = new Cookie();
exactly same type and checking, just written once
nothing decided at run type (java statically typed)
where does var work / not work
local variables, for & for-each variables, try-with-resources, all w initializer
fields, method params, return types, constructor params, catch params
when to use var
when right hand side names the type
keyword vs reserved type name; what is var
like class, new or int; can’t be used as a name
“infer the type”; only where type is expected; var
what does private belong to
class, not location
copyOf
returns a defensive copy
nobody outside can add/remove/replace original
callers get own copy, box keeps the original
what does assigning an array do
only copies the arrow, whereas copyOf makes second version
when do rules in setters apply
only when code calls that setter
how is a static member accessed
it is reached through the class
static vs final
one copy shared by class & every object
assigned once, never reassigned
pass by value vs pass by reference
method gets COPY; change param → caller is unaffected (Java)
method gets caller’s VARIABLE; reassign → caller sees new value
if in the method you change the parameter, what is primitive and what is object reference? what about if you call a method on it? or assign a new object to it?
caller unaffected; caller unaffected
n/a, object CHANGES;
n/a, caller unaffected
can change object, cannot change which object caller holds
call by sharing
callee shares caller’s OBJECT, but not caller’s VARIABLE
toString vs println
RETURNS a String, prints nothing
does printing after calling toString
@Override
compiler checking your work
without it, misspelled method = brand new method never called
with it, compiler checks that you are actually overriding smth
even though code runs w/o it, write it anyways
overloaded constructors
give caller the shapes they need
same name (must be class name) & diff param lists
compiler picks by looking at args you pass
every constructor has to what
update the counter
constructor chaining
process of calling one constructor from another within same class to avoid code duplication
(this(); must be first line) or from parent / superclass (super()))
what counts as diff param list for number, type, order, and return type
(double) and (double, boolean) - fine
(double) and (String) - fine
(String, int) and (int, String) - fine but confusing
format specifiers
%s : any value as text (ex. “2.3”)
%d : a whole number (ex. 2)
%f : decimal, six places (ex. 1.200000)
%.2f : decimal, two places (ex. 1.20)
%% : literal percent sign (%)
superclass vs subclass
parent vs child
classes linked by extends
super() vs super(__, __, __, …)
calls no arg constructor
passes shared values up to constructor
super(…) vs super.___
calls superclass CONSTRUCTOR; allowed only as first statement of constructor
reaches superclass version of method or field, skipping any override; allowed in any instance method
single inheritance
Java only allows one parent
upcast vs downcast
storing subclass object in superclass variable
automatic and always safe
casting superclass reference to subclass type
needs a cast; can fail at run time
only use to reach method the parent class doesn’t have
getClass() vs getSimpleName()
gives object class including package name
gives just class name
how to decide whether to cast
use instanceOf
class
template/blueprint for creating objects
object
software model built from class (an instance)
hierarchy
inheritance relationships between classes
super
refers to parent
super(…) calls its constructor
super.x reaches its member
parameterized constructor
takes values to set up new object
diamond problem
with two parents, which inherited version of method should be used?