Java Applet,Abstract Class,Abstracts Method,Interface


# What Is Java Applet?
ð  An applet is a small Internet-based program written in Java, a programming language for the Web, which can be downloaded by any computer. The applet is also able to run in HTML. The applet is usually embedded in an HTML page on a Web site and can be executed from within a browser.

There are some important differences between an applet and a standalone Java application, including the following:
• An applet is a Java class that extends the java.applet.Applet class.
• A main() method is not invoked on an applet, and an applet class will not define main().
• Applets are designed to be embedded within an HTML page.
• When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine.
• A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment.
• The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime.
• Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as Sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed.
•Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.

Life Cycle of an Applet:

Four methods in the Applet class give you the framework on which you build any serious applet:

Init(): This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.

Start(): This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.

Stop(): This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.

Destroy(): This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.

Paint(): Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.
The paint() method is responsible for drawing (or "painting") the applet onto the screen for the user to see. The paint() method takes one argument of declared type Graphics:
public void paint(Graphics g) {


Synchronization deadlocks in Java programs

Deadlocks can occur in Java because the synchronized keyword causes the executing thread to block while waiting for the lock, or monitor, associated with the specified object. Since the thread might already hold locks associated with other objects, two threads could each be waiting for the other to release a lock; in such a case, they will end up waiting forever. The following example shows a set of methods that have the potential for deadlock. Both methods acquire locks on two lock objects, cache Lock and table Lock, before they proceed. In this example, the objects acting as locks are global (static) variables, a common technique for simplifying application-locking behavior by performing locking at a coarser level of granularity:

Abstract Class:
-     An abstract class is a class that is declared abstract.
-      It may or may not include abstract method.
-      A class must be compulsorily labeled abstract , if it has one or more abstract methods.
-      It cannot be instantiated.
-      It can be sub classed.
-      When an abstracted class is subclass, the subclass usually provides implementations for all the abstracts method in its parent class .However if it does not, the sub class must also be declared abstract.method in its parent class .However if it does not, the sub class must also be declared abstract.
Abstracts Method:                                   

 - An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon).
-If a class includes abstract methods ,the class itself must be declared abstract ,as in-

public abstract class Maruf(){
    declare fields
    declare non-abstract methods
    abstract void draw();
}
                                                                                                                                                                    
Interface:
An interface is not a class.
- An interface has no implementation.
- It only has the signature or in other words, just the definition of the methods without the body.
- When we create an interface, we are basically creating a set of method without any implementation that must be overridden by the implemented classes.

# the main difference between Interface and Abstract class is that a class can implement more than one interface but can only inherit from one abstract class.
-Since class doesn’t support multiple inheritances, interfaces are used to implement inheritance.
# All of the methods in an interface are implicitly abstract, So the abstract modifier is not used with interface methods. (It could be, it’s not necessary)

1 comment: