Midterm Exam Flashcards
object
data and all stuff you can this use data for/with
class
specify objects, encapsulate data and methods together
Encapsulation
allows for access restrictions (“data hiding”) through non-public methods and non-public data
– allow users specific access
public vs private vs protected
- any class can access public methods/variables
- Private methods/variables can only be accessed by the class they are declared in.
- Protected methods and variables can only be accessed by the class they are declared in, or any subclasses of that class.
constructors
create instances of classes (AKA objects)
data abstraction
- the user of a class knows the nature of the data and the public methods, but not the implementation details
The user does not need to know how something works!
encapsulation VS data abstraction
Encapsulation allows for implementation details (data and methods) to be restricted
Data abstraction allows for a user to use the class effectively without having to know these implementation details
static
static methods and variables are not part of the Objects that classes create
– rather they are part of the Class themselves.
final
final variables, methods, and classes are constant
***disallows overrides
- final variables can’t be reassigned
- final methods can’t be overridden
- final classes can’t be subclassed
references
Other than primitive types, all Java variables are references
- a variable that holds the memory address of an object. – points to the location where the object’s data is stored.
.equals()
compares references or contents of Strings
subclasses
can call the constructor of a superclass from a subclass
when to use an overriding method?
When a subclass defines a method with:
– The same name
– The same number and type of parameters
– The same return type
As a method in the superclass, then the subclass is overriding the superclass’s method.
If you want to call the overridden method from the subclass…
use super
if you omit super?
the method calls itself, which calls itself, which repeats until the program crashes (StackOverflow error)
What if we don’t want our methods to be overridden?
disallow overrides by marking methods as final
The Object Class
- Every object we work with extends from Object
- contains certain methods including: toString, equals, clone
- Most cases we want to override these methods
toString()
returns a String representation of the Object
equals()
equals(Object other)
– returns a boolean whether the current Object is equal to other.
clone()
returns a copy of the current Object
What happens if the Object we want to clone() contains other Objects as data fields?
– we need to copy (shallow or deep)
shallow copy
- just assign those references to the new Object.
– both copies will have references to the same nested ____ object
ex/ Tea class has a field called ‘milk’ which is of type String
– Doing a shallow copy of myTea will produce a separate Tea Object that points to the same milk String that the original myTea does.
**if modified, the shallow copy points will also be affected bc they are the same Object
deep copy
all nested objects must also be copied
ex/ Doing a deep copy of myTea will also deep copy the objects it points to, so Milk also gets copied.
** if modified, the copy remains unchanged
abstract methods/classes
-Abstract methods cannot be private, static, or final
- Any class with at least one abstract method must be declared as an abstract class
- Constructors cannot be abstract