Week 6 Flashcards
ORM Framework features, advantages, and disadvantages
- Object Relational Mapping is a tool that uses objects to connect the OOP language and database systems
- Benefits - ORM maps an object to the table, can hide the details of SQL queries from OO logic, provides methods for automatic versioning and timestamping, provides caching support for better performance, best suited for large projects, injected transaction management, configurable logging, faster development of applications
- some ORM tools are Hibernate, JPA, Active JPA, iBATIS, IBM Pure Query, etc
What methods are available in the Object class (4)?
.clone
.hashcode
.equals
.toString
How would you clone an object?
First, tag the class with the Cloneable marker interface.
Next, invoke clone().
The clone method is declared in java.lang.Object and does a shallow copy.
What is an enhanced for loop?
Enhanced for loops allow easier traversal of Collections (actually any arrays or Iterables)
Syntax - for (object x : collection){. . .}
What is try-with-resources?
What interface must the resource implement to use this feature?
- a Java 7 feature
- allows for automatically closing resources in a try/catch block using try(resource) {…} syntax
- Must implement the AutoCloseable interface
How would you make numbers in your code more readable?
Use the underscore ( _ ) for numeric literals; must be placed between numbers (EX - 45,000 = 45_000)
What are covariant return types?
A method is allowed to return objects that are child classes of the return type.
Also, when overriding a method, the return type of the new method can be a child class of the original return type
What are 3 usages of the super() keyword?
- to refer to immediate parent class instance variable.
- super() is used to invoke immediate parent class constructor (also can pass params)
- to invoke immediate parent class method.
Can you overload / override a main method? static? private? default? protected?
Main method - overload, cannot override b/c is static method.
Static method - overload, cannot override b/c belongs to class (not inherited).
Private method - overload, cannot override b/c doesn’t get inherited.
Default method - both.
Protected method - both (override if inherited).
What is the difference between FileReader and BufferedReader?
FileReader is just a Reader which reads a file, so it reads characters and uses the platform-default encoding.
BufferedReader reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines (e.g. can read one line at a time).
You can wrap a BufferedReader around a FileReader
How would you pass multiple values with a single parameter into a method?
Use varargs
What is static block?
- used for static initialization
- executed only once - upon creation of the first object of a class or access to static methods of a class
What are static imports?
a static method or variable from a class - syntax: import static
How would you clone an object?
use the .clone() method inherited from object class
What makes a class immutable (6)?
- Declare the class as final so it can’t be extended.
- Make all fields private so that direct access is not allowed.
- Don’t provide setter methods for variables.
- Make all mutable fields final so that it’s value can be assigned only once.
- Initialize all the fields via a constructor performing deep copy.
- Perform cloning of objects in the getter methods to return a copy rather than returning the actual object reference.
If 2 objects are equal, do they have the same hashcode? If not equal?
If two objects have the same hashcode then they are NOT necessarily equal.
But if objects are equal, then they MUST have same hashcode.
What data types are supported in switch statements (6)?List all non-access modifiers?
- byte
- char
- enums
- int
- short
- string
List all non-access modifiers (9)?
- abstract
- default
- final
- native
- static
- strictfp
- synchronized
- transient
- volatile
Which collections cannot hold null values (4)?
HashTable
TreeSet
ArrayDeque
PriorityQueue
What is the .forEach() merthod in Java 8?
The forEach() method accepts what is called a functional interface as its parameter (specifically a Consumer), which the lambda expression ( -> ) then implements at runtime.
If 2 interfaces have default methods and you implement both, what happens?
The code will NOT compile unless you override the method.
However, the code WILL compile if one interface is implemented further up in the class hierarchy than the other - in this case, the closest method implementation in the hierarchy will be called.
If 2 interfaces have same variable names and you implement both, what happens?
The code will compile unless you make a reference to the variable (this is an ambiguous reference).
You must explicitly define the variable by using the interface name: int a = INTERFACENAME.a;
Why does HashTable not take null key?
The HashTable hashes the keys given as input, and the null value cannot be hashed
What new syntax for creating variables was introduced with Java 10?
The var keyword, with type inference