Google

FAQs in JAVA

Following are some questions which are frequently asked in job interviews as well as in other academic courses in Java technology.

1. Method Overloading Vs. Method Overriding

Method overriding refers to over-riding the functionality of a method in the parent class. The rules for method overriding are:-

  • Overriding method must have same signature as overridden method
  • Overriding method's return type MUST be the same as that of overridden method's return type.
  • Overriding method's throws clause must be compatible.
  • ANY METHOD CAN THROW UnChecked exception without declaring.

Method overloading refers to a class having more than one method with same name but different parameter list. (Note that this makes their signature different). There are no rules. In affect, overloaded methods are entirely independent of each other. Remember, if signature is different, it is a different method.

2. Parameter Passing By-Reference and By-Value

In parameter passing-by-reference approach , the reference of the variable is passed to the formal argument of the subroutine. So, the value of the variable used in calling method also gets modified.

In parameter passing-by- value approach, a copy of value is made from actual to formal method. So, the value of the variable in calling program remains unaffected.

3. Method super( ) Vs. keyword super

The method ‘super()’ is used to invoke a superclass constructor.
The keyword ‘super’ allows access to things in the superclass that are hidden by things in the subclass. For example, super.x always refers to an instance variable named x in the superclass. This can be useful for the following reason: If a class contains an instance variable with the same name as an instance variable in its superclass, then an object of that class will actually contain two variables with the same name: one defined as part of the class itself and one defined as part of the superclass. The variable in the subclass does not replace the variable of the same name in the superclass; it merely hides it. The variable from the superclass can still be accessed, using super.

When you write a method in a subclass that has the same signature as a method in its superclass, the method from the superclass is hidden in the same way. We say that the method in the subclass overrides the method from the superclass. Again, however, super can be used to access the method from the superclass.

4. final, finalize( ), finally( )

Final: This has different meaning when applied to variables and when to methods and classes.

- If ‘final’ is used before a variable definition then the value of the variable cannot be modified.
- If ‘final’ is applied to a class then it prevents creation of derived classes from this class i.e. inheritance is banned.
- If ‘final’ is used before a method then this method cannot be overridden in the derived classes.

Finally: This keyword is used in conjunction with try-catch block. However, the code within the ‘finally’ block is executed, no matter, exception is thrown or not.

Finalize: The code in the ‘finalize()’ method is executed when the program terminates. This is different from the destructor in the sense that destructor is executed every time the object goes out-of-scope but finalize is executed only once.

5. throw Vs. throws

Basically, "throw" means: something's gone wrong, crash my program unless somebody up the stack thought this might happen, catches it, and does something else.

However, "throws" is a tag for methods, to warn people up the stack to be prepared.

6. Wait Vs. Sleep

‘Wait’ causes a thread to be in suspended mode until an event occurs for which the thread is waiting.

‘Sleep’ suspends a thread for a known specified duration.

7. Interface Vs. Class

Interface’ contains only the member variables and declaration of the member functions. It does not contain implementation of these methods. The implementation is done in the class that implements this interface. So, an interface may have many implementations.


On the other hand, a class is a complete entity in itself. It has member variables and methods declaration and implementation as well.

8. Init Vs. Start methods of an applet

Init’ method contains code needed for initialization of the applet while ‘Start’ method starts the actual execution of the applet.

9. Applet Vs. Application

An application is a standalone java program which requires no supporting software for their execution whereas an applet requires supporting software (like web browser or ‘appletviewer’ utility) for their execution.

10. String Vs. StringBuffer

A string created as String class object cannot be modified. If modification is required then a new string has to be created.
However, a string created with StringBuffer class has methods to modify the string.

Recommended Books

  • Java 2 Complete Reference by Patrick Naughton & Herbert Schildt
  • J2EE Unleashed by Paul Allen and Joseph J Bambara
  • Codenotes for j2ee by Gregory Brill

Recommended Websites

  • http://www.javapassion.com
  • http://java.sun.com