Java Reference Types (Questions)
- What is the difference between an Interface and an Abstract class?
An abstract class declaration has at least one instance method that is declared abstract, and this method will be implemented by the subclasses. An abstract class can have instance methods that implement a default behavior.
An Interface can only declare constants and instance methods, but cannot implement default behavior.
- What is the purpose of garbage collection in Java, and when is it used?
The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it can no longer be referenced by the program in which it is used.
- Describe synchronization with respect to multithreading.
With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.
- Explain the different ways of using a thread?
A thread can be implemented by using the runnable interface or by extending the Thread class.
The former is more advantageous, because when you are trying to achieve multiple inheritance this must be achieved using interfaces.
- What is the difference between pass by reference and passby value?
Pass By Reference means passing the address itself rather than passing the value. “Passby Value” means passing a copy of the value to be passed. In Java, primitives are passed by value and objects are passed by reference.
- What is the difference between a HashMap and a Map?
A Map is an interface and a Hashmap is a class that implements the Map interface.
- What is the difference between a HashMap and HashTable?
The HashMap class is roughly equivalent to Hashtable except that it is unsynchronized and allows null values. (The HashMap allows null values as key-value pairs whereas the Hashtable does not). The HashMap does not guarantee that the order of the map will remain constant over time.
The Hashmap is not synchronized and the Hashtable is synchronized.
- What is the difference between Vector and ArrayList?
Vector is synchronized whereas arraylist is not.
- What is the difference between Swing and Awt?
The AWT represents heavy-weight components and Swing represents light-weight components. Hence Swing works faster than AWT.
- What is the difference between a constructor and a method?
A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class that has its own name, a return type (which may be void), and is invoked using the dot operator.
- What is an Iterator?
Some of the collection classes provide traversals of their contents via a java.util.Iterator interface.
This interface allows you to walk a collection of objects, operating on each object in turn. Iterators contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.