Java Flashcards
What is the difference between an interface and an abstract class?
Interface
- Interface can have only abstract, default and static methods
- The interface keyword is used to declare an interface
- The implement keyword is used to make a class abide by the methods defined by the interface
- Interface supports multiple inheritance
- Interface can’t provide the implementation of abstract class
- Interface has only static and final variables
- All method declarations are implicitly public
- You can implement any number of interfaces
Abstract
- Abstract class can have abstract and non-abstract methods
- Abstract class doesn’t support multiple inheritance
- You can declare fields that have final, non-final, static and non-static variables
- Abstract class can provide the implementation of methods, have concrete methods.
- The abstract keyword is used to declare abstract class
- All fields are automatically public, static, and final
- All methods that you declare or define are public
- You can extend only one class, whether or not it is abstract
Both
- Can not be instantiated
- Can be subclassed
- If the subclass does not provide the implementation for the parent class abstract methods, the subclass must be declared abstract.
What are abstract methods?
An abstract method is a method that is declared without an implementation.
What are default methods?
Default methods enable you to add new functionality to the interfaces by specifying the method definition and using the default keyword as the method signature
What is an interface class?
Interface is a reference type and is a contract that binds specific method signatures and fields to the class that implements it
What is an abstract class?
An abstract class is a class that is declared abstract. It may or may not include abstract methods.
What is the diamond problem?
There is a class A. Both class B and C derives from class A. Thus both class B and class C implements method from class A. Now we have class D. Class D derives from class B and C. The diamond problem comes from when class D makes a call to method definitions in class A. This call is ambiguous because it is not sure whether to call the version of the method derived from class B or class C.
Java does not have the diamond problem since it does not support multiple inheritance. Instead, Java uses interfaces.
What is the static keyword?
Variables that are declared static is called a class variable that is initialized one and all instances share the same copy of the variable. It can be accessed directly without creating an instance of the class. It can be accessed directly by the class name and doesn’t need any object.
What is an instance variable
A variable without the static keyword and each instance of the class has its own copy of the variable.
What is the difference between an ArrayList and a Vector?
Vector is synchronized, which means only one thread is working and no other thread can perform an operation at a time.
Arraylist is not synchronized, so multiple threads can work on ArrayList at the same time.
Both ArrayList and a Vector can grow and shrink dynamically, but the way they resize is different. Vector double itself by ArrayList grow by half of its size.
ArrayList is faster since it is non-synchronized and Vectors are slower because it is thread-safe since the Vector gets locked until the work is complete by the thread.
An ArrayList is fail-fast since it is not thread safe unlike a Vector
What is fail-fast?
Fail-fast is an exception that gets thrown when a collection is being iterated and it detects some changes being made to the collection.
What is the difference between a tree set and a hashset?
HashSet
- Better performance than Tree Set, offers constant time
- Does not maintain any order of elements
- Store contents according to the key, or the hash code. The hash code is used as an index
Tree Set
- Takes log(n) time
- Sort in ascending order by default
Both
- Extends AbstractSet and implements the Set interface
- Does not hold duplicate elements
- It is more optimal to add elements to HashSet and convert it into Tree Set for sorted elements.
- Both are non-synchronized, so they are not thread-safe
What is a hashcode?
A number generated from an object. It is used to store and retrieve objects quickly in a hashtable.
What type of Collections that does not allow duplicates?
Set
A set is a Collection that cannot contain duplicate elements.
What is the difference between HashTable and HashMap?
HashMap
- It is not synchronized, so it is not thread safe
- HashMap allows one null key and any number of null values
- HashMap object values are iterated by using iterator
- Iterator is fail-fast
- HashMap is much faster and uses less memory than HashTable since it is not synchronized
HashTable
- It is synchronized, so it is thread safe
- HashTable do not allow null keys and null values
- HashTable is the only class other than Vector which uses enumerator to iterate the object values
- Not fail-fast
- Slower performance since it is synchronized
- Legacy
How do you serialize (persist) an object in Java?
Java has the Serialization API
Serialization is the process of saving an object’s state to a persistence store and also rebuilding the object from the save information when needed in the future.
To serialize an object
- Implement the Serializable interface. This is consider a marker interface and have no default implementation. The interface is simply an indication to the Java runtime that the object is valid for persistence
- Use the writeObject(someObject object) method from the ObjectOutputStream class.
- To restore the object, you use the readObject() method from the ObjectInputStream class and you will need to cast the object to the specific class.
- Only the object state is saved
What is a stored procedure and how would you call it in Java?
A stored procedure is an executable block of code that is written in PL/SQL and stored in the Oracle database. A stored procedure is called from a Java class using CallableStatement object. When the procedure is called, its name and any relevant parameters are sent over the JDBC connection to the DBMS which executes the procedure and returns the result (if applicable) via the connection.