[go: up one dir, main page]

0% found this document useful (0 votes)
6 views7 pages

What Is Java

A Java Applet is a small Java application that runs in a web browser, offering benefits like platform independence and a secure execution environment. The applet life cycle includes methods such as init(), start(), paint(), stop(), and destroy(). Additionally, the document explains abstract methods, method overriding, and provides examples of Java programs demonstrating these concepts.

Uploaded by

manishyt500
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

What Is Java

A Java Applet is a small Java application that runs in a web browser, offering benefits like platform independence and a secure execution environment. The applet life cycle includes methods such as init(), start(), paint(), stop(), and destroy(). Additionally, the document explains abstract methods, method overriding, and provides examples of Java programs demonstrating these concepts.

Uploaded by

manishyt500
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1. What is Java Applet? Write its benefits. Also explain the life cycle of an applet.

Answer:

A Java Applet is a small application written in Java, that runs in a web browser or an applet viewer. It is
embedded in an HTML page and can be run using a Java-enabled browser.

Benefits of Java Applets:

1. Platform-independent.

2. Secure execution environment (sandbox).

3. GUI support through AWT and Swing.

4. Can run on client-side without installation.

5. Ideal for dynamic and interactive web pages.

Applet Life Cycle:

The applet life cycle includes the following methods:

1. init() – Called once when the applet is loaded.

2. start() – Called after init() and every time the applet becomes active.

3. paint(Graphics g) – Invoked to redraw the applet.

4. stop() – Called when the browser leaves the applet page.

5. destroy() – Called when the applet is about to be removed from memory.

import java.applet.*;

import java.awt.*;

public class MyApplet extends Applet {

public void init() {

System.out.println("Applet initialized");

public void start() {

System.out.println("Applet started");
}

public void paint(Graphics g) {

g.drawString("Hello Applet!", 50, 50);

public void stop() {

System.out.println("Applet stopped");

public void destroy() {

System.out.println("Applet destroyed");

2. Define concept of “implementation of abstract method”. Write a program to implement abstract


method.

Answer:

In Java, abstract methods are methods without a body, declared in an abstract class. They must be
overridden in the subclass.

Concept:

Abstract methods define what a class should do, but not how.

Any class inheriting an abstract class must provide concrete implementation of all abstract methods.

Program:

abstract class Animal {

abstract void sound();

class Dog extends Animal {

void sound() {
System.out.println("Dog barks");

class TestAbstract {

public static void main(String args[]) {

Animal obj = new Dog();

obj.sound();

3. What is method overriding? Write any program to explain the concept of method overriding.

Answer:

Method Overriding in Java occurs when a subclass provides a specific implementation of a method that is
already defined in its superclass.

Rules:

Same method name.

Same parameter list.

Different class (subclass and superclass).

Inheritance is a must.

Example:

class Animal {

void sound() {

System.out.println("Animal makes a sound");

}
class Dog extends Animal {

void sound() {

System.out.println("Dog barks");

class TestOverride {

public static void main(String[] args) {

Animal a = new Dog(); // Runtime polymorphism

a.sound();

This is an example of runtime polymorphism using method overriding.

4. Write a Java program using class and object to find area of rectangle.

Answer:

class Rectangle {

int length;

int breadth;

void insert(int l, int b) {


length = l;

breadth = b;

void calculateArea() {

int area = length * breadth;

System.out.println("Area of rectangle: " + area);

public class TestRectangle {

public static void main(String args[]) {

Rectangle r1 = new Rectangle();

r1.insert(10, 5);

r1.calculateArea();

Explanation:

Class: Rectangle has properties length and breadth.

Method insert() initializes values.

Method calculateArea() computes area.

Main method creates object and calls methods.

5. Explain “abstract method” with syntax and suitable example.


Answer:

An abstract method is a method that is declared without implementation. It must be declared in an


abstract class, and subclasses must provide implementation.

Syntax:

abstract class ClassName {

abstract return_type methodName();

Example:

abstract class Vehicle {

abstract void start();

class Car extends Vehicle {

void start() {

System.out.println("Car starts with key");

public class Main {

public static void main(String[] args) {

Vehicle v = new Car();

v.start();

}
}

Key Points:

Cannot be instantiated.

Must be inherited.

Used for achieving abstraction.

You might also like