Difference between an Interface and an Abstract class
- All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and non-abstract methods.
- A class may implement a number of Interfaces, but can extend only one abstract class.
- In order for a class to implement an interface, it must implement all its declared methods. However, a class may not implement all declared methods of an abstract class. Though, in this case, the sub-class must also be declared as abstract.
- Abstract classes can implement interfaces without even providing the implementation of interface methods.
- Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
- Members of a Java interface are public by default. A member of an abstract class can either be private, protected or public.
- An interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be instantiated, but can be invoked if it contains a main method.
How do you ensure that N threads can access N resources without deadlock ?
- A very simple way to avoid deadlock while using N threads is to impose an ordering on the locks and force each thread to follow that ordering. Thus, if all threads lock and unlock the threads in the same order, no deadlocks can arise.
Differences exist between Iterator and ListIterator
- An Iterator can be used to traverse the Set and List collections, while the ListIterator can be used to iterate only over Lists.
- The Iterator can traverse a collection only in forward direction, while the ListIterator can traverse a List in both directions.
- The ListIterator implements the Iterator interface and contains extra functionality, such as adding an element, replacing an element, getting the index position for previous and next elements, etc.
Differences exist between HashMap and Hashtable
- A HashMap allows the existence of null keys and values, while a Hashtable doesn’t allow neither null keys, nor null values.
- A Hashtable is synchronized, while a HashMap is not. Thus, HashMap is preferred in single-threaded environments, while aHashtable is suitable for multi-threaded environments.
- A HashMap provides its set of keys and a Java application can iterate over them. Thus, a HashMap is fail-fast. On the other hand, a Hashtable provides an Enumeration of its keys.
Difference between ArrayList and LinkedList
- An ArrayList is an index based data structure backed by an Array. It provides random access to its elements with a performance equal to O(1). On the other hand, a LinkedList stores its data as list of elements and every element is linked to its previous and next element. In this case, the search operation for an element has execution time equal to O(n).
- The Insertion, addition and removal operations of an element are faster in a LinkedList compared to an ArrayList, because there is no need of resizing an array or updating the index when an element is added in some arbitrary position inside the collection.
- A LinkedList consumes more memory than an ArrayList, because every node in a LinkedList stores two references, one for its previous element and one for its next element.
HASHSET
HashSet does not guarantee any insertion orders of the set but it allows null elements. HashSet can be used in place of ArrayList to store the object if you require no duplicate and don’t care about insertion order.
Method Overloading Rules
- change method signature. Method signature is made of number of arguments, type of arguments and order of arguments
- only changing the return type of method does not amount to method overloading.
- So your overloaded method throws the same exception, a different exception or it simply does no throw any exception; no effect at all on method loading.
Method Overriding Rules
- argument list in overridden and overriding methods must be exactly same
- return type of overriding method can be child class of return type declared in overridden method.
public class SuperClass { //Overriden method public Number sum(Integer a, Integer b) { return a + b; } } class SubClass extends SuperClass { //Overriding method @Override public Integer sum(Integer a, Integer b) { //Integer extends Number; so it's valid return a + b; } }
private, static and final methods can not be overridden
Overriding method can not throw checked Exception higher in hierarchy than thrown by overridden method. Let’s say for example overridden method in parent class throws FileNotFoundException, the overriding method in child class can throw FileNotFoundException; but it is not allowed to throw IOException or Exception, because IOException or Exception are higher in hierarchy i.e. super classes of FileNotFoundException.