0% found this document useful (0 votes)
10 views15 pages

Untitled Document

Uploaded by

puneetsaboo6
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)
10 views15 pages

Untitled Document

Uploaded by

puneetsaboo6
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/ 15

✅ Unit IV – Java Programming Notes (Definitions + Q&A)

---

⚡ Key Definitions
---

Class

A class is a blueprint for objects. It defines variables and methods that objects of the class will
have.

---

Object

An object is an instance of a class that occupies memory and can perform operations defined in
the class.

---

Method

A block of code that performs a specific task and runs when called.

---

Method Overloading

Multiple methods in the same class having the same name but different parameters.

---

Method Overriding

Defining a method in a child class with the same name and parameters as in the parent class.
---

Final Variable

A constant whose value cannot be changed after initialization.

---

Final Method

A method that cannot be overridden by subclasses.

---

Final Class

A class that cannot be inherited by other classes.

---

Static Variable

A variable shared by all objects of a class.

---

Static Method

A method that belongs to the class rather than any object. Called using class name.

---

Visibility Control (Access Modifiers)

Specifies how accessible a class member is:

private – accessible only in the same class


default – accessible in the same package

protected – accessible in same package + subclasses

public – accessible everywhere

---

Array

A container that holds multiple values of the same type in contiguous memory locations.

---

String

A sequence of characters treated as an object in Java.

---

Interface

A collection of abstract methods (no body) that a class implements. Used for achieving multiple
inheritance.

---

Inheritance

The concept of acquiring properties and methods from another class.

---

Single Inheritance

One class inherits from one superclass.


---

Multiple Inheritance

A class inherits from multiple classes (Java supports this via interfaces, not classes).

---

Abstract Class

A class with one or more abstract methods that has no implementation and must be
implemented by subclasses.

---

Abstract Method

A method without a body, declared using abstract keyword.

---

Package

A group of related classes and interfaces.

---

API Package

Standard libraries provided by Java (like java.util, java.lang).

---

Thread

A lightweight process. Allows parallel execution of code.

---
Multithreading

Running multiple threads simultaneously to perform tasks faster.

---

Extending Thread Class

Creating a new thread by inheriting from the Thread class.

---

Runnable Interface

An interface implemented to create threads without extending Thread class.

---

Thread Lifecycle

States through which a thread passes: New → Runnable → Running → Blocked → Terminated.

---

Stopping a Thread

Method to stop a running thread (e.g. stop() – deprecated in Java).

---

Blocking a Thread

A thread that waits for a condition to become true or for resources to be released.

---

Exception
An unwanted event that disrupts normal program flow.

---

Types of Errors

Compile-time Error: Syntax errors caught by compiler.

Runtime Error: Errors occurring during program execution.

Logical Error: Program runs but produces incorrect result.

---

Checked Exception

Exceptions checked at compile-time (e.g., IOException).

---

Unchecked Exception

Exceptions not checked at compile-time (e.g., ArithmeticException).

---

Exception Handling

Mechanism to handle runtime errors to prevent program crash.

---

try-catch Block

Used to handle exceptions gracefully.


---

finally Block

Block that always executes whether exception occurs or not.

---

Syntax of Exception Handling

try {
// code that may throw exception
} catch (Exception e) {
// exception handling code
} finally {
// clean-up code
}

---

Applet

A small Java program that runs inside a browser.

---

Applet Lifecycle

init()

start()

paint()

stop()

destroy()

---
HTML Applet Tag

Used to embed an applet in a web page.

Example:

<applet code="HelloApplet.class" width="300" height="300"></applet>

---

Graphics Class

Java class used to draw shapes and text in applets.

---

Drawing Methods in Graphics

drawLine()

drawRect()

drawOval()

drawString()

drawArc()

---

---

✅ Important Questions & Answers


(Exactly as provided earlier for your exam preparation – included here for completeness.)

---
1. What are Classes and Objects in Java?

A class is a blueprint or template for creating objects. It defines properties (variables) and
behaviors (methods).

An object is an instance of a class, which uses memory and can perform tasks defined in the
class.

Example:

class Student {
int id;
String name;
void display() {
System.out.println(id + " " + name);
}
}

public class Test {


public static void main(String[] args) {
Student s1 = new Student();
s1.id = 101;
s1.name = "Puneet";
s1.display();
}
}

---

2. Explain Method Overloading with example.

Method overloading means multiple methods in the same class with the same name but
different parameters.

Example:

class MathOperations {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}

---

3. What is Method Overriding?

Method overriding means redefining a parent class method in the subclass.

Example:

class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}

class Dog extends Animal {


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

---

4. Explain final variable, final method and final class.

final variable – value can’t be changed after assignment.

final method – can’t be overridden in child class.

final class – can’t be inherited.

Example:

final class A {
final int x = 10;

final void show() {


System.out.println("Hello");
}
}

---

5. What are static members in Java?

static members belong to the class instead of instances. You can access them without creating
objects.

Example:

class Example {
static int count = 0;
static void display() {
System.out.println("Count is: " + count);
}
}

---

6. Difference between abstract class and interface.

Abstract Class​Interface

Can have methods with body​All methods abstract (Java 7)


Can have constructors​ Cannot have constructors
Use abstract keyword​Use interface keyword

---

7. What is an Interface in Java? How do you implement it?

Example:

interface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() {
System.out.println("Drawing Circle");
}
}

---

8. What is a Package? How to import it?

A package is a collection of classes and interfaces.

Creating a package:

package mypackage;

public class A {
public void display() {
System.out.println("Inside package");
}
}

To import:

import mypackage.A;

---

9. How to create a Thread in Java?

Two ways:

By extending Thread class

By implementing Runnable interface

Example:

class MyThread extends Thread {


public void run() {
System.out.println("Thread running...");
}
}

---

10. What is Exception Handling? Explain try-catch block.

Example:

try {
int data = 100 / 0;
} catch (ArithmeticException e) {
System.out.println("Can't divide by zero");
}

---

11. Explain types of Exceptions in Java.

Checked Exception: caught at compile-time (e.g., IOException)

Unchecked Exception: caught at runtime (e.g., ArithmeticException)

---

12. Syntax of try-catch-finally block.

try {
// risky code
} catch (Exception e) {
// handling
} finally {
// cleanup
}

---

13. What is an Applet? How to write and run it?


Example:

import java.applet.Applet;
import java.awt.Graphics;

public class HelloApplet extends Applet {


public void paint(Graphics g) {
g.drawString("Hello Applet", 50, 50);
}
}

To run:

Compile: javac HelloApplet.java

Run using appletviewer or embed in HTML.

---

14. What is the use of paint() method in Applet?

paint(Graphics g) is used for drawing shapes and text in an applet.

---

15. How to draw shapes in Applet (circle, rectangle, etc.)?

Example:

public void paint(Graphics g) {


g.drawLine(20, 30, 80, 30);
g.drawRect(100, 40, 60, 30);
g.drawOval(200, 50, 50, 50);
}

---

16. How to add an Applet to HTML Page?

Example:
<applet code="HelloApplet.class" width="300" height="300">
</applet>

---

17. Explain stop(), start(), and run() methods in Thread.

start() → starts the thread.

run() → code executed by thread.

stop() → forcibly stops thread (deprecated).

You might also like