Friday 23 December 2016

Interview Questions based on Method Overloading and Method Overriding

What is method overriding?

Modifying a super class method in the sub class is called method overriding. Using method overriding, we can change super class method according to the requirements of sub class.

What are the rules to be followed while overriding a method?

  • Must have the same argument list.
  • Must have the same return type, except that as of Java 5, the return type can be a subclass—this is known as a covariant return. 
  • Must not have a more restrictive access modifier.
  • May have a less restrictive access modifier.
  • Must not throw new or broader checked exceptions.
  • May throw fewer or narrower checked exceptions, or any unchecked exception.

Can we override static methods.? Explain the answer.

No, Static methods can not be overridden.
Explanation: Static method are not associated with any object.Even if we invoke any static method of the class on any object, at compile time, complier replaces the reference variable with the class name, therefore they can not be overridden.



What happens if we change the arguments of overriding method?

If we change the arguments of overriding method, then that method will be treated as overloaded not overridden.

Can we override protected method of super class as public method in the sub class?
Yes. We can change the visibility of overriding methods, but we can only increase it and can’t reduce it.

Can we change the return type of overriding method from Number type to Integer type?

Yes. The return type of the overriding method can be a sub-type of return type of super class(also knows as co-variance).

Can we override a super class method without throws clause as a method with throws clause in the sub class?

Yes, but only with unchecked type of exceptions.

Can we change an exception of a method with throws clause from NullPointerException to ArrayIndexOutOfBoundException while overriding it?

Yes. Overridden method may throw NullPointerException or it’s sub class exception or any unchecked type of exceptions.

What is the use of super keyword?

Using super keyword, we can refer super class version of overridden method in the sub class.

Can we override private methods?

No, we can not override private methods because private method is not visible in the sub class.

Can we remove throws clause of a method while overriding it?

Yes. You can remove throws clause of a method while overriding it.

What is method overloading?
When a class has more than one method with same name but different parameters, then we call those methods are overloaded. Overloaded methods will have same name but different number of arguments or different types of arguments.

What is method signature? What are the things it consist of?
Method signature is used by the compiler to differentiate the methods. Method signature consist of three things.
  •  Method name
  •  Number of arguments
  •  Types of arguments
Can we declare one overloaded method as static and another one as non-static?
Yes. Overloaded methods can be either static or non static.

How do compiler differentiate overloaded methods from duplicate methods?
Compiler uses method signature to check whether the method is overloaded or duplicated. Duplicate methods will have same method signatures i.e same name, same number of arguments and same types of arguments. Overloaded methods will also have same name but differ in number of arguments or else types of arguments.

Is it possible to have two methods in a class with same method signature but different return types?
No, compiler will give duplicate method error. Compiler checks only method signature for duplication not the return types. If two methods have same method signature, straight away it gives compile time error.

Can overloaded methods be synchronized?
Yes. Overloaded methods can be synchronized.
Can we overload main() method?
Yes, we can overload main() method. A class can have any number of main() methods but execution starts from public static void main(String[] args) only.

Can we declare overloaded methods as final?
Yes, we can declare overloaded methods as final.


Saturday 3 December 2016

Intermediate Level(1-3 yrs ) Interview Question based on Java Collections

The collection framework of Java is asked in almost every Java interview. In the last post I shared some of the collection questions which are asked the fresher level or below 1 year of experience. If you haven't gone through that here is the link for that.Java Collection Interview Question(0-1 Years). Below are some of questions that are asked at some experience level.

 

What is the difference between ArrayList and Vector ?


Vector is synchronized while ArrayList is not . Vector is slow while ArrayList is fast . Every time when needed, Vector increases the capacity twice of its initial size while ArrayList increases its ArraySize by 50%.

 

What is the difference between HashMap and Hashtable ?


a. HashMap allows one null key and any number of null values while Hashtable does not allow null keys and null values.
b. HashMap is not synchronized or thread-safe while Hashtable is synchronized or thread-safe .

 

What is the difference between peek(),poll() and remove() method of the Queue interface ?


Both poll() and remove() method is used to remove head object of the Queue. The main difference lies when the Queue is empty().
If Queue is empty then poll() method will return null . While in similar case , remove() method will throw NoSuchElementException .
peek() method retrieves but does not remove the head of the Queue. If queue is empty then peek() method also returns null.

 

What is the difference between Iterator and ListIterator.


Using Iterator we can traverse the list of objects in forward direction . But ListIterator can traverse the collection in both directions that is forward as well as backward.

 

What is the difference between Array and ArrayList in Java ?


a. Array is static in size while ArrayList is dynamic in size.
b. Array can contain primitive data types while ArrayList can not contain primitive data types.When you add any primitive data type to ArrayList, it will automatically convert it to its corresponding Wrapper class(auto Boxing).

 

What is the difference between HashSet and TreeSet ?


a.  HashSet maintains the inserted elements in random order while TreeSet maintains elements in the sorted order
b. HashSet can store null object while TreeSet can not store null object.

 

What is the difference between HashMap and ConcurrentHashMap ?


Main differences between HashMap and ConcurrentHashMap are :
a. HashMap is not synchronized while ConcurrentHashMap is synchronized.
b. HashMap can have one null key and any number of null values while ConcurrentHashMap does not allow null keys and null values .

 

What is the difference between LinkedList and ArrayList in Java ?


Main differences between LinkedList and ArrayList are :
a. LinkedList is the doubly linked list implementation of list interface , while , ArrayList is the resizable array implementation of list interface.
b. LinkedList can be traversed in the reverse direction using descendingIterator() method  provided by the Java Api developers , while , we need to implement our own method to traverse ArrayList in the reverse direction.

 

Why Map interface does not extend the Collection interface in Java Collections Framework ?


Map interface is not compatible with the Collection interface.
Explanation : Since Map requires key as well as value , for example , if we want to add key-value pair then we will use put(Object key , Object value) . So there are two parameters required to add element to the HashMap object  . In Collection interface add(Object o) has only one parameter.
The other reasons are Map supports valueSet , keySet as well as other appropriate methods which have just different views from the Collection interface.

 

When to use ArrayList and when to use LinkedList in application?


ArrayList has constant time get operation O(1).Hence, ArrayList is preferred when the list is collection required more querying than modifying.
Insertion , Deletion operations take constant time O(1) for LinkedList. Hence, LinkedList is preferred when there are more insertions or deletions involved in the application.

Suggested Posts:

Interview Questions on List Interface