CMSC 132 Material

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/182

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

183 Terms

1
New cards
Encapsulation
the hiding of implementation details
2
New cards
Data Encapsulation
way data is stored not visible and don't know data structure (ArrayList, Array? idk)
3
New cards
Procedural Encapsulation
doesnt give instructions for procedure / method
4
New cards
Java Interface
contains method prototypes for classes to implement
5
New cards
can interface have instance variables?
no
6
New cards
can interface have static final variables?
yes
7
New cards
can interface have static method?
yes can have implementation (InterfaceName.method());
8
New cards
can interface have instance methods?
yes, need DEFAULT **public default void land(){...}
9
New cards
why do we use interfaces?
polymorphism,,, can have interface as parameter for method, polymorphic collections
10
New cards
polymorphic collections (interface)
an interface name can be for an array ... CanEat[] eaters \= {new Dog(), new Owl(), ...};
11
New cards
wrapper class
a class designed to contain a primitive data type so that the primitive type can behave like a reference type (EX ARRAY LISTS)
12
New cards
initiating wrapper objects (integer with number 7)
Integer x \= Integer.valueOf(7); or Integer x \= 7
13
New cards
autoboxing
the automatic conversion between a primitive value and a corresponding wrapper object

ex. Integer x \= 7;

**7 is primitive but boxed / wrapped in object
14
New cards
Autounboxing
the automatic conversion between wrapper object to corresponding primitive value

ex.

Integer y \= 3;
Integer z \= y+ 2;

**unbox obj y, add to 2, box again to make object z
15
New cards
collections class
contains static methods that work on most collections
16
New cards
collections method to shuffle list
Collections.shuffle(list);
17
New cards
collections method to find max in list
Collections.max(list)
must be comparable
18
New cards
collections method to find min in list
Collections.min(list)
must be comparable
19
New cards
collections method to sort list
Collections.sort(list)
must be comparable
20
New cards
collections method to complete a binary search (index where thing is in list)
Collections,binarySearch(list, num to be found)

list must already be sorted
21
New cards
collections method to reverse a list
Collections.reverse(list);
22
New cards
Java enumerations
-a lot like a class
-has small number of pre-defined instances with symbolic constants
23
New cards
how do we compare enum variables?
\==
24
New cards
how to access enum variable
EnumerationName.VARIABLE
25
New cards
how to create array from enums
EnumerationName.values()
26
New cards
how to get index of enum
EnumerationName.Variable.ordinal();
27
New cards
how are enumerated types like a class?
can have instance members(variables & methods)

can have static members(variables and methods)
28
New cards
how are enumerated types different from a class?
constructors PRIVATE
29
New cards
how to write enumerated types with enums and variables
ex. SUN
name variable "Sunday"
int workHours \= 0
SUN("Sunday",0);
private String name;
private int workHours;

\---contructor
30
New cards
writing a copy contructor for class Party
with variables name, max guests
public Party(Party party){
this.name \= party.name;
this.maxGuests \= party.maxGuests;
}

**REMEMBER MAY NEED TO CREATE COPY FOR PRIVACY LEAKS
31
New cards
Iterator
an object containing data and methods to iterate through a collection of data, allowing processing of one data item at a time.
32
New cards
Iterable
interface that has ability to provide you with an interator, implemented by collection
33
New cards
is a for each loop an interator?
YES
34
New cards
why do we need iterator / iterable types?
only way to cycle through ANY Java Collection
35
New cards
create an iterator to go through a Integer ArrayList
Interator
36
New cards
what methods do you have to use to iterate through a list and remove elements
while(iterator.hasNext)

iterator.next()
iterator.remove();
37
New cards
Immutable Class/Object
state (instance variables) set during contruction but cannot be modified later
38
New cards
Mutable Class / Object
state is set during contruction
39
New cards
privacy leak
When someone OUTSIDE a class has access to a private reference INSIDE the class
40
New cards
defensive copy
Technique when getter method returns a copy of the mutable object.

public Mutable(StringBuilder b) {
builder \= new StringBuilder(b);
}
public StringBuilder getBuilder() {
return new StringBuilder(builder);
}
41
New cards
writing comparable method
implement Comparable
42
New cards
implement vs extend
ONLY EXTEND 1 CLASS

CAN IMPLEMENT MULTIPLE INTERFACES
43
New cards
Student is a subclass of Person. Which is correct?

44
New cards
this vs super
this --\>reference to current object

super --\> reference to superclass

**super does same thing as this except the class right above on inheritance tree
45
New cards
inheritance memory diagram
\
\
46
New cards
subclass constructor
call super constructor
call super constructor
47
New cards
equals method
check if this \== obj
check if ! instanceOf
cast as class
compare and return
48
New cards
object class
-object class extended if no other specified
-always root of inheritance tree
-object methods: toString() equals() getClass()
49
New cards
class vs type
-object object has exactly one instance of a class
-class of an object cannot change
-objects may satisfy many types
50
New cards
Person p \= new GradStudent();

What is the type of the variable?
person
51
New cards
Person p \= new GradStudent();

What is the class of the object?
GradStudent
52
New cards
Person p \= new GradStudent();

What are the type(s) of the obect? (IS-A)
Gradstudent, Student, person
53
New cards
overloading
2 methods same name dif parameters (return type doesn't matter)
54
New cards
overriding
same name, same parameter types
-in subclass, "replaces" inherited method
-return type may be more specific (ex original returned is Person, now is Student)
-visibility may be increased(public --\> adding extra feature)
55
New cards
Early (static) binding
makes decision at compile time, only sees type of variable
56
New cards
Late (dynamic) binding
makes decision at runtime and looks at the class of the object
what java uses
57
New cards
dynamic dispatch
what to invoke is decided during run time based on type.
58
New cards
casting...when will it compile?
if cast is above or below on inheritance tree
59
New cards
casting...when will it run?
when object satisfies type (IS-A)
60
New cards
Upcasting
referencing an object as an instance of a more general parent class, do not have to specify, done implicitly.
61
New cards
Downcasting
casting a superclass to a subclass...never done implicitly!
62
New cards
Class Cast Exception
An exception thrown when an object variable is cast with an incompatible class.
63
New cards
safe downcasting
use instance of to check if variable instance of that class
64
New cards
multiple inheritance
A derived class inheriting class members from two or more base classes

**not supported by java
65
New cards
Inheritance vs Composition
Inheritance defines what objects are and hierarchial nature of that strucutre IS A Composition defines how objects interact HAS A

\*\*favor composition if you dont need polymorphism, composition has much more flexible design
Inheritance defines what objects are and hierarchial nature of that strucutre IS A Composition defines how objects interact HAS A

\*\*favor composition if you dont need polymorphism, composition has much more flexible design
66
New cards
final
1) final variable

2) final in front of object variable declaration (variable cant change but object can change)

3. final class (dont want any class to extend)

4. final method (cannot override / modify)
67
New cards
public visibility
can be referenced anywhere
68
New cards
protected visibility
Any class in the same package or any class derived can reference it (public would violate encapsulation). Provides the best possible encapsulation that permits inheritance.
69
New cards
package visibility
only classes in same classes can access
70
New cards
private visibility
can be used anywhere inside the class definition but cannot be referenced externally
71
New cards
Shadowing
override variable thats inherited...dont do it
72
New cards
abstract class
a class that defines attributes and methods for subclasses but is never instantiated

can be extended , instance variables, abstract methods
73
New cards
unchecked exception
- errors and run time exceptions
- result of programming errors
AssertionError, VirtualMachineError, ArithmeticException, NullPointerException, IndexOutOfBoundsException
74
New cards
checked exception
-must "catch or declare"
1. handle with catch block
2. declare that method throws it
75
New cards
Comparator Interface
Allows for multiple and different comparisons for ordering of sorting. has one method compare(a,b)

use by saying

Collections.sort(list, new ClassThatImplementsComparator());
Allows for multiple and different comparisons for ordering of sorting. has one method compare(a,b)

use by saying

Collections.sort(list, new ClassThatImplementsComparator());
76
New cards
implementing a comparator
Collections.sort(list, new ClassThatImplementsComparator());
77
New cards
static nested class
-like having 2 classes
-both can access private features of the other
-usually nested private and helps the outer class
78
New cards
inner class
\-privacy not respected -each inner class must be "associated with" a particular outer class object -NOT STATIC -B object knows everything abt A object, A doesnt usually know B exists (cannot edit B) -Parasite(B), Host(A) -B hooked onto "this" current object
\-privacy not respected -each inner class must be "associated with" a particular outer class object -NOT STATIC -B object knows everything abt A object, A doesnt usually know B exists (cannot edit B) -Parasite(B), Host(A) -B hooked onto "this" current object
79
New cards
instantiating inner class object

\** A is outer class and B is inner class
A outerObject \= new A();
A.B innerObject \= outerObject.new B();
80
New cards
inner class for iterator
outer class implements Iterable
81
New cards
Anonymous Inner Class
class with no name declared and instantiated in same spot inside a class must implement or extend -no constructors
class with no name declared and instantiated in same spot inside a class must implement or extend -no constructors
82
New cards
Anonymous inner class syntax
new Foo(){...};

\** foo interface or class extending
83
New cards
lambda expression
-used to pass code to method
-javas attempt at being "Functional Language"
-used in a method with a parameter thats a functional interface type
-lambda expression implements sole method from a the functional interface
84
New cards
functional interface
any interface that contains only one abstract method

USE FOR LAMBDA EXPRESSION
ex. comparator, comparable, runnable, action listener
85
New cards
lambda exppression syntax
(a, B) -\>{ ...};
-NO TYPES ON VARIABLES
-NAME OF METHOD INFERRED FROM FUNCTIONAL INTERFACE
86
New cards
if you have an argument for a method that is a functional interface
use lambda expression!!!!!!!!!!!!!!!!!!!!!!!
87
New cards
why lambda expression better than anonymous class
-slightly less memory
-runs slightly faster
-sometimes fewer objects at runtime
-reduces need for garbage collector
88
New cards
shallow copy
Copying only the reference to an object.
Copying only the reference to an object.
89
New cards
deep copy
a copy that completely duplicates the contents of the source object to a destination object
a copy that completely duplicates the contents of the source object to a destination object
90
New cards
clone method
inherited from object class

provides very fast shallow copy of entire object
has a checked exception --\> CloneNotSupported
91
New cards
clone method for an immutable address object return
return (Address)super.clone();

**CATCH CHECKED EXCEPTION
CloneNotSupportedException
92
New cards
clone() method with mutable component
save copy of object, make clone of individual mutable component
93
New cards
how to make a class cloneable
1. only possible if suprtclass is Object or implements cloneable
2. include "implements cloneable"
3.carefully override the clone() method
94
New cards
static initialization block
Code is automatically executed when the class is loaded (once at the very beginning of program execution) to initialize all static variables
Code is automatically executed when the class is loaded (once at the very beginning of program execution) to initialize all static variables
95
New cards
Instance initialization block
executed everytime object initiated before constructor
executed everytime object initiated before constructor
96
New cards
gui
Graphical user interface;
interface between user and software that includes both input and output
97
New cards
main thread
executes main method and then dies

sends request to EDT to render and initiate GUI, SHE DEAD
98
New cards
Event Dispatch Thread (EDT)
handles user interaction,

responds to events,

certain events cause EDT to run code in listeners
99
New cards
Event-driven programming
some program statements run when triggered by an event, like a mouse click or a key press
100
New cards
container
rectangular region contains widgets