cnit255 exam2

0.0(0)
Studied by 2 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/74

encourage image

There's no tags or description

Looks like no tags are added yet.

75 Terms

1
New cards

paradigm

  • way of framing the problem

  • one description, many things made from it

  • OOP


2
New cards

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


3
New cards

abstraction

  1. class (description)

  2. data

  3. how are they related


4
New cards

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


5
New cards

overloading

  • ONE class has several methods with same name

  • different param lists chosen by compiler


6
New cards

keyword extends

one class (first one) reuses another (second one)

7
New cards

inheritance vs composition

  • IS a: write extends

  • HAS a: a field

    • important because java allows one direct superclass per class


8
New cards

encapsulation

bundling data (variables) and its methods into a class while restricting direct access from outside the class

  1. declare class variables as private to hide from direct external modifications

  2. provide public getter/setter methods for controlled reading/updating of these variables


9
New cards

public field vs private getter and setter

  • cannot refuse anything

  • decides what it will accept


10
New cards

order of structure types when writing class

  1. fields

  2. constructors

  3. methods


11
New cards

what naming convention do class names use

PascalCase (every word capitalized, including the first)

12
New cards

fields you do not set are…

empty

13
New cards

int, long, short, byte default value

0

14
New cards

float, double default value

0

15
New cards

char default value

‘\u0000’ (not digit zero)

16
New cards

boolean default value

false

17
New cards

any object type default value

null (including String)

18
New cards

local variable default value

none

  • reading it is compile error

  • if there is none, probably has not been initialized


19
New cards

accessibility modifier & what are fields and methods usually

decides what code elsewhere is allowed to see

  • fields private, methods public


20
New cards

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


21
New cards

camelCase

variables and methods

22
New cards

SCREAMING_SNAKE

constants

23
New cards

instantiation

making an object

ex. Cookie cookie = new Cookie();

  • Cookie = type, cookie = reference var, new Cookie() makes object


24
New cards

what does the variable hold

reference, not the object

25
New cards

label the components of: oopclasses.Cookie@1dbd16a6

oopclass.Cookie = full class name

@ = separator

1dbd16a6 = identity hash code

  • NOT a memory address nor pointer


26
New cards

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


27
New cards

signature

name and parameter types

28
New cards

can you overload on return type

no

  • compiler would have nothing to tell them apart by


29
New cards

what does the dot operator do

reach inside an object

  • instance method needs an instance, which is what the dot is for


30
New cards

two parts in accessing / modifying a private field

  1. getter / accessor

    1. returns field type, no parameters

  2. setter / mutator

    1. returns void, takes one parameter


31
New cards

write getter & setter for Baker

public String getBaker() {
    return baker;
}

public void setBaker(String baker) {
    this.baker = baker;
}


32
New cards

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


33
New cards

what happens if you remove “this” in a setter

compiles perfectly, does nothing at all

  • parameter assigned to itself


34
New cards

constructor name and return type

same name as class, no return type

<p>same name as class, no return type</p>
35
New cards

what does the keyword new do (4 steps)

  1. new allocates memory onto the heap

  2. every field set to default (0, false, null)

  3. constructor body runs, replacing defaults

  4. new returns reference to finished object


36
New cards

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


37
New cards

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


38
New cards

how many constructors can you have

infinite, as long as param lists differ

39
New cards

keyword static

belongs to class, not object

40
New cards

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


41
New cards

constructor vs static factory method

  • always returns NEW object, name fixed to class

  • can return existing object, can have descriptive name


42
New cards

java is pass by…

value

43
New cards

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)


44
New cards

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


45
New cards

when to use var

when right hand side names the type

46
New cards

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


47
New cards

what does private belong to

class, not location

48
New cards

copyOf

returns a defensive copy

  • nobody outside can add/remove/replace original

  • callers get own copy, box keeps the original


49
New cards

what does assigning an array do

only copies the arrow, whereas copyOf makes second version

50
New cards

when do rules in setters apply

only when code calls that setter

51
New cards

how is a static member accessed

it is reached through the class

52
New cards

static vs final

  • one copy shared by class & every object

  • assigned once, never reassigned


53
New cards

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


54
New cards

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


55
New cards

call by sharing

callee shares caller’s OBJECT, but not caller’s VARIABLE

56
New cards

toString vs println

  • RETURNS a String, prints nothing

  • does printing after calling toString


57
New cards

@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


58
New cards

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


59
New cards

every constructor has to what

update the counter

60
New cards

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()))


61
New cards

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


62
New cards

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 (%)


63
New cards

superclass vs subclass

parent vs child

  • classes linked by extends


64
New cards

super() vs super(__, __, __, …)

  • calls no arg constructor

  • passes shared values up to constructor


65
New cards

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


66
New cards

single inheritance

Java only allows one parent

67
New cards

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


68
New cards

getClass() vs getSimpleName()

  • gives object class including package name

  • gives just class name


69
New cards

how to decide whether to cast

use instanceOf

70
New cards

class

template/blueprint for creating objects

71
New cards

object

software model built from class (an instance)

72
New cards

hierarchy

inheritance relationships between classes

73
New cards

super

refers to parent

  • super(…) calls its constructor

  • super.x reaches its member


74
New cards

parameterized constructor

takes values to set up new object

75
New cards

diamond problem

with two parents, which inherited version of method should be used?