1/118
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
application programming interface (api)
description of what’s available in a class to the user
api examples
String, public features
data encapsulation
hides the representation of data from the user; the implementation details of a class are hidden from the public
procedural encapsulation
hides the algorithmic procedure; allows user to use system without worrying about specifics behind how the system works
autoboxing
Integer x = 7;
autounboxing
System.out.println(x+2);
comparable
interface that can be implemented to sort objects in “natural order”
comparable syntax
interface Comparable<T> {
public int compareTo(T other);
}
what are some java libraries that use comparable?
Collections.sort, Collections.max, Collections.min, Collections.binarySearch, Arrays.sort, Arrays.binarySearch
what similarities/differences to enumerations have to classes?
similarities: both have instance + static members
differences: constructors for enums are always private!
enumeration syntax
public enum Day {
SUN, MON, TUES, WED, THURS, FRI, SAT;
}
iterator
object that can sequentially access the elements of a collection
iterable interface
collections that implement this interface provide an iterator that can access its own elements
t/f: arraylists are iterable
true
write an iterator for the following arraylist: ArrayList<Cat> list = new ArrayList<>();
Iterator<Cat> iterator = list.iterator();
iterable interface syntax (include the three methods typically implemented in this interface)
interface Iterator<E> {
boolean hasNext();
E next();
void remove();
}
t/f: for-each loops are iterators
true
why do we need iterators/for-each loops?
1) the only way to cycle through elements of any java collection
2) efficient way to remove elements
immutable class/object
instance variables set during construction but cannot be modified after that
mutable class/object
instance variables set during construction and might be modified later
what happens when a mutable data member has been encapsulated?
modification of its state within methods of the class is possible
direct mutation by external code is not possible
what is a privacy leak? come up with an example that generates a privacy leak, and then practice correcting it
when private mutable data is accidentally exposed
what is inheritance?
creating new classes based on existing ones
keyword: extends
what is always at the root of the inheritance tree?
Object
what is polymorphism?
allows user to define one interface and implement it in various ways
suppose A extends B and B extends C. consider the method below:
void foo(B x) {...}
a. what classes of objects can be passed as arguments to this method?
b. what methods (from which classes) can be called on x inside this method?
what is method overloading?
methods in class w/ same name but different parameter types
what is method overriding?
subclass “replaces” inherited method (same parameter types)
what is “this”?
keyword used to refer to current object in a method/constructor
how is “this” used in constructors?
used to initiate a constructor in a current class
how is “this” used outside of constructors?
what is “super”?
used to invoke constructors/methods/instance variables of immediate parent class
how is “super” used in constructors?
how is “super” used outside of constructors?
early (static) binding
compiler makes decision (what is the type of variable?)
early binding example
Person p = new Student();
p.walk();
with early binding, walk() will be called via the Person class
late (dynamic) binding
decision made at runtime (what is class?)
late binding example
Person p = new Student();
p.walk();
with late binding, walk() will be called via the Student class
which type of binding does java use?
late (dynamic) binding
upcasting
casting “up the tree”; done implicitly
downcasting
casting “down the tree”
never done implicity
Person p = new Person();
Student s = new Student();
Person tricky = new Student();
will the following code compile?
Person y = s;
yes, implicit upcast
Person p = new Person();
Student s = new Student();
Person tricky = new Student();
will the following code compile?
Student y = p;
no, attempted implicit downcast
Person p = new Person();
Student s = new Student();
Person tricky = new Student();
will the following code compile?
Student y = tricky;
no, attempted implicit downcast
Person p = new Person();
Student s = new Student();
Person tricky = new Student();
will the following code compile?
(Student)p
complies, but throws exception
Person p = new Person();
Student s = new Student();
Person tricky = new Student();
will the following code compile?
(Student)tricky
yes
Person p = new Person();
Student s = new Student();
Person tricky = new Student();
will the following code compile?
(Faculty)s
no, student + faculty on same level of tree
Person p = new Person();
Student s = new Student();
Person tricky = new Student();
will the following code compile?
(Faculty)tricky
compiles, but throws exception
which type of casting is done implicitly?
upcasting
what’s a safe form of downcasting (using the teacher + student classes)?
Student s = (Student)p;
if (p instanceOf Student) {
Student s = (Student)p;
}
what’s the purpose of safe downcasting?
prevents exceptions (in the case that p (Person) refers to object that does not satisfy type Student)
what is the visibilty of public int x?
x is visible anywhere in project
what is the visibilty of private int x?
x is visible only in current class
what is the visibilty of int x (package visibility)?
x is visible in any class within current package
what is the visibilty of protected int x?
x is visible in any class within the current package + any subclass of the current class
will the underlined method compile?
public class Base {
public void m (int x) { … }
}
public class Derived extends Base {
public int m (int x) {…}
}
no
will the underlined line of code compile?
public class Base {
public void m (int x) { … }
}
public class Derived extends Base {
public void m (int x) {…}
}
override
will the underlined line of code compile?
public class Base {
public void m (int x) { … }
}
public class Derived extends Base {
public void m (double x) {…}
}
overload
which of the numbered method is called?
public class Base {
public void m (int x) { … }
}
public class Derived extends Base {
public int m (int x) { … } (1)
public void m (int x) { … } (2)
public void m (double x) { … } (3)
}
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
x.m(5);
1 (superclass)
which of the numbered method is called?
public class Base {
public void m (int x) { … }
}
public class Derived extends Base {
public int m (int x) { … } (1)
public void m (int x) { … } (2)
public void m (double x) { … } (3)
}
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
y.m(6);
2 (override in subclass)
which of the numbered method is called?
public class Base {
public void m (int x) { … }
}
public class Derived extends Base {
public int m (int x) { … } (1)
public void m (int x) { … } (2)
public void m (double x) { … } (3)
}
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
y.m(7.0);
won’t compile
which of the numbered method is called?
public class Base {
public void m (int x) { … }
}
public class Derived extends Base {
public int m (int x) { … } (1)
public void m (int x) { … } (2)
public void m (double x) { … } (3)
}
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
z.m(8.0);
3 (overload in subclass)
x == y
true when x and y are the SAME object (aliases)
x.equals(y)
true when x and y are objects that “look” the same (same state)
equals() is called extensively throughout __________ framework
collection
is multiple inheritance allowed in java?
no!
can you override a variable that’s inherited
yes, it’s called (shadowing) (not recommended)
what are the benefits of abstract classes (in comparison of interfaces)
instance variables (state)
constructors
what is the benefit of interfaces (in comparison of abstract classes)
a class can implement more than one
what’s the purpose of the keyword final?
final int x = 7; → x cannot be modified
final Cat c = new Cat(); → c cannot refer to a different Cat
final void foo {…} → cannot override/create subclass
abstract class
cannot be instantiated directly
can’t use “new”
purpose: to be extended in different ways
unchecked exceptions
things that should never happen
typically programming errors
usually don’t require try/catch handlers
checked exceptions
represent common events that can occur even w/ correct implementation
normally require a catch block (catch/declare):
handle w/ catch block
declare method that throws it
unchecked exception class syntax
public class IllegalFishPositionException extends RuntimeException {…}
checked exception class syntax
public class NetworkTooSlowException extends Exception {…}
static nested classes syntax
public class A {
public static class B {
}
}
inner nested class syntax
public class A {
public class B {
}
}
what’s the difference between static nested classes and inner nested classes?
static classes: essentially like having two ordinary classes
inner classes: instance of inner class is always “attached to” an instance of the outer class
“shares” its members
no privacy between A + B
instantiating inner class objects syntax
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
comparator interface
separate class (implementing Comparator) to define “additional orderings”
comparator interface syntax
interface Comparator<T> {
int compare(T obj1, T obj2);
}
anonymous inner classes
class w/ no name
declared + instantiated in the same spot
must implement an interface or extend a class
cannot have constructors
if declared in static method, object in not “attached to” any instance of outer class
functional interface
interface w/ just one abstract method
functional interface examples
BinaryFunction, Comparator, Comparable, Runnable, ActionListener
lambda expressions
objects that contain code for a single method
no state, exactly "one behavior”
can assign to variable whose type is a functional interface
lambda expressions syntax
BinaryFunction multiplication = (a, b) → {
return a*b;
};
what are some advantages lambda expressions have over anonymous inner classes?
uses (slightly) less memory
runs (slightly) faster
sometimes fewer objects at runtime (reduces need for garbage collection)
what are two ways to copy an object
1) copy constructor
2) clone() method (if class implements Cloneable)
clone()
inherited from Object class
returns a very fast shallow copy of entire object
class of new object is same as the class of the original
implements Cloneable interface
no methods to implement
clone() syntax
public Triangle clone() throws CloneNotSupportedException {
return (Triangle)super.clone();
}
when does a static initializer block run?
runs once, when class is first loaded
static initializer syntax
public class Foo {
static {
}
}
instance initializer syntax
public class Foo {
{
}
}
when does a instance initializer block run?
runs every time an object is instantiated
runs before the constructor
can eliminate redundancy in constructors
reference copy
two variables point to exact same object in memory
reference copy example
Person p = new Person();
Person refCopy = p;
shallow copy
make a new object, but make reference copies for the different instance variables
shallow copy example
int[] a = {1, 2, 3};
int[] shallowCopy = a.clone();