Question : When do you declare a method as abstract method ?
answer : When we want child class to implement the behavior of the method.
Question : Can I call a abstract method from a non abstract method ?
answer : Yes, We can call a abstract method from a Non abstract method in a Java abstract class.
Question : What is the difference between an Abstract class and Interface in Java ? or can you explain when you use Abstract classes ?
answer : Abstract classes let you define some behaviors; they force your subclasses to provide others. These abstract classes will provide the basic funcationality of your applicatoin, child class which inherited this class will provide the funtionality of the abstract methods in abstract class. When base class calls this method, Java calls the method defined by the child class.
- An Interface can only declare constants and instance methods, but cannot implement default behavior.
- Interfaces provide a form of multiple inheritance. A class can extend only one other class.
- Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
- A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
- Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast.
- Neither abstract classes not interface can be instantiated .
answer : All predefined exceptions in Java are either a checked exception or an unchecked exception. Checked exceptions must be caught using try .. catch() block or we should throw the exception using throws clause. If you dont, compilation of program will fail.
Java Exception Hierarchy
+--------+
| Object |
+--------+
|
|
+-----------+
| Throwable |
+-----------+
/ \
/ \
+-------+ +-----------+
| Error | | Exception |
+-------+ +-----------+
/ | \ / | \
\________/ \______/ \
+------------------+
unchecked checked | RuntimeException |
+------------------+
/ | | \
\_________________/
unchecked
Question : How you can force (call) the garbage collection to happen ?
answer : Garbage collection is an automatic process , we can call garbage collector in Java by
calling System.gc() and Runtime.gc() , JVM tries to recycle the objects , but there is
no guarantee when it happens.
Question : What are the ways to create a thread ? which one has the advantage ?
answer : There are 2 ways of creating a new thread.
1) Define a new class that extends Thread class .2) Define a new class that implements the runnable interface and pass an
object of that class to a thread's constructor .
There is an advantage with the second approach . If we create using first approach
since we are extending from Thread class there is no possibility to inherit or extend
from any other class whenever there is an necessary (as we can extend from only
one class) . If we go for the second approach to create a thread since we are
implementing runnable thread there is still a chance to inherit or extend any other
class other than thread class .
Question : how many methods does runnable interface has ?
answer : A runnable interface has only one method i.e. is run ( )
public void run();
So every class that implements runnable interface must provide body or logic for
run method .
Question : How do you know the state of a thread?
answer : getstate( ) method is used to know the state of a thread .
Question : What is a Servlet ?
answer : servlet is a java program which resides and executes on server side , to create
dynamic HTML web pages . A servlet can handle multiple requests concurrently .
Question : What is the hierarchy of servlet package ?
answer :
servlet
|
Generic Servlet
|
HTTPServlet
|
MyServlet
Every servlet implements this servlet interface dirctly or indirectly .
Question : What is the difference between HTTPServlet and Generic Servlet ?
answer : An Generic Servlet is used for handling any type of requests (may be FTP , HTTP )
where as HTTpServlet is used to handle only for HTTP requests . A Generic Servlet
has service( ) method to handle the request where as HTTpServlet has doGet() and
doPost() methods to handle the request .
Question : What is the difference between servlet and applet ?
answer : Servlet is an java program which executes on server side where as applet is also an
java program which executes on client side browser . However unlike applets ,
servlets do not have graphical user interface .
Question : What is the difference between doGet() and doPost() method ?
answer : Whenever we invoke a servlet the servlet engine passes the information to the
servlets service( ) method . This method determines the type of request made
(GET , POST) and calls the doGet or doPost methods respectively . if the request is
get then we can able to pass 2K data from HTML , all data we are passing to server
will be displayed in URL (request string i.e. can see in the address bar of the
browser) . If the request is post then there is no size limitation , all data passed to
the server will be hidden user cannot see this info on browser .
Question : Explain the Servlet life cycle ?
answer : When a request comes the init () method of the servlet is first invoked , next the
service () method to handle the request and destroy () method is called to remove
servlet from the pool.
Hello Friends , i would like to share with you what i know about java .
ABOUT
- JAVA is an object oriented programming language
- It was created by James gosling
- Initially the language was called OAK later changed to JAVA.
- Initially Java was made to make the machines intelligent like microwave ovens , by embedding an software in to the machine .
- Later when internet came into picture there were only text based web pages .
- So Sun Microsystems thought of creating interactive web pages and so introduced APPLETS.
- JAVA became the default language to develop APPLETS.
- APPLETS were invented but no browser was able to run them . So Sun Microsystems has released their own browser called HOT JAVA which has the ability to execute APPLETS.
- Now every browser can execute APPLETS , as JAVA is an open source.
- RUBY ON RAILS an OOP language used for developing web applications is an strong competitor for JAVA .
- There are three editions in JAVA J2SE , J2ME , J2EE .
- J2SE - Java Standard Edition often known as core Java is used for developing APPLETS .
- J2ME - Java Micro Edition is used to develop MIDLETS , for mobiles and electronic appliances.
- J2EE - Java Enterprise Edition is used for developing SERVLETS .