1. What is Generic Servlet ?
Answer : Defines a generic, protocol-independent servlet. GenericServlet implements the Servlet and ServletConfig interfaces. GenericServlet makes writing servlets easier. It provides simple versions of the lifecycle methods init and destroy. To use Protocol specific servlet like http protocol, we can use HttpServlet. Difference in HttpServlet and GenericServlet is that, HttpServlet provides few more methods like doGet and doPost. Same operations can be done in service method of generic servlet. Read Servlet Life cycle
2. Can abstract class have Constructor ?
Answer : Yes. abstract class can have a constructor.
3. We cannot instantiate the abstract class, then what is the need of constructor and how it is called.
Answer : You may have not come across this situation. but answer i very easy. If want to initialize few parameters for all the subclasses then we can use the constructor of abstract class. Constructor calling mechanism is same as normal inheritance in java. Default constructor gets called automatically and to call other constructors we have to explicitly call it by using keyword “super”.
4. Does java have virtual function ?
Answer : As such there is no keyword “virtual” present in java. but except static, final and private method all the methods are virtual bu default. Refer this article to read more on it.
5. How you will create custom tag in JSP ?
Answer :
- Create tld file in “WEB-INF/tlds” folder and configure required properties.
- Create a class which implements interface Tag / BodyTag or extends class TagSupport / BodyTagSupport. In that class write getter and setter for defined property. Few methods required by interface are doStartTag(), doEndTag() and release(). Read how to create custom tag in detail.
6. What is Externalization ?
Answer : Externalization is the interface provided by the JAVA which extends interface Serializable. It provides two more methods as compared to Serializable interface –
- void readExternal(ObjectInput in)
- void writeExternal(ObjectOutput out)
The above two methods gives the flexibility and control over how the object is serialized and deserialized. for further read refer this article.
7. What is the use of serialVersionUID in JAVA ?
Answer : Whenever object is created, one unique ID is associated with object and its implementation may defer from compiler to compiler. Whenever any object is serialized and deserealized, this variable is used to determine whether the object is in same state or not ? assignment of default serialVersionUID is mostly depends upon the methods and fields present in class and if any field is added or removed from the class definition then its serialVersionUID also changes and if the value does not match then it results in “InvalidClassException”. Because of this reason it is recommended to have a default unique serialVersionUID with each serialized undergoing class. To read in detail with example, refer this article.
8. If i don’t want to serialize some fields in class, then how to achieve this ?
Answer : This question can also be asked as “What is the transient variable ?”. “transient variables and static variables” can be used to avoid the field being serialized in Java. (As static variable is class level variable, and it does not associated at object level – it is not serialized).
9. What will happen if one of the members in the class doesn’t implement Serializable interface?
Answer : If you try to serialize an object of a class which implements Serializable, but the object includes a reference to an non- Serializable class then a “˜NotSerializableException‘ will be thrown at runtime.
10. If a class is Serializable but its super class in not, what will be the state of the instance variables inherited from super class after deserialization?
Answer : Java serialization process only continues in object hierarchy till the class is Serializable and values of the instance variables inherited from super class will be initialized by calling constructor of Non-Serializable Super class during deserialization process .
11. What is the conceptual difference between interface and abstract class ? When to use interface and abstract class ?
Answer :
Abstract class and its usage :
A class which is partially implemented and have few abstract methods are know an “abstract class“. few developers think that why anybody should create a partial implementation ? The best example i have in my mind is “DataMapper Pattern”. For the CRUD operation – open connection and close connection and sequence of save method will be known to the programmer however which query will be executed to save or retrieve differs for different object. in that case createInsertSQL() method can be made abstract and internally save() will call openConnection() and closeConnection() which is implemented. below code snap explains the save() method.
abstract public String createInsertSQL();//abstract method public void save() //method implemented { try{ openConnection();//method implemented String sql = createInsertSQL(); //abstract method executeStatement();//method implemented } catch(SQLException ex){ } finally{ closeconnection();//method implemented } }
As you can see in above code snap, only unknown implementation is the SQL Query which is made abstract and others are implemented. and therefore underlying child class must implement the method createInsertSQL() and call save().
Interface and its usage :
interface are also known as “contract”. when we know the skeleton/ structure of the class but it complete implementation differs then we must use interface. in following scenario we must use interface :
- Dependency injection
- Runtime polymorphism
In design pattern programming to interface is preferable rather than programming to implementation (inheritance).
12. How to iterate over keys of HashMap in JDK 4 and 5?
Answer : This is the common question asked in interview.
In JAVA 5 : we can use advance for loop as shown in above code, use map.keySet(). This will return the Set (As Keys must be unique)
In JAVA 4 : use map.keySet() and get the Iterator object using map.iterate() . then using while loop , get the value for each key.
13. What is Exception chaining and how to achieve ?
Answer : When one exception causes another exception then the second exception must print the log of the first exception also.
We can use initcause() method of the “Throwable” class to retain the cause of exception.
Leave a Reply