is it possible applet and application both can retain in a single code ? in fact when i need applet i will use applet and when i need application i will use application . can they stay both in a sigle code ?
The only real difference between an Applet and an application is the environment they run in. An Applet runs in a "sandboxed" environment in a browser, an application runs in a JVM with no such restrictions. If your Applet and application have common functionality it makes sens to create this in a set of shared classes so they can be invoked from either.
Typically, when I create GUI applications, I create one class that extends JFrame and another that extends JPanel. The frame class has minimal logic; it adds the panel to its content pane and creates a menu (if I want one). The JPanel class on the other hand has most of the logic to create the GUI controls (buttons, text fields, labels, etc.). This allows me to easily create a third class that extends JApplet and just adds the already-written panel with most of the GUI logic set up. I only have to provide an alternative to the menu that was in the JFrame class. In a program of any major complexity, there are also other classes that do the actual work of the program. It's usually a good idea to separate the "business logic" from the "GUI logic" as much as possible.
To clarify my written explanation above, here's a quick example of some code: