Java
Java
---
**Theory**:
- **Rules**:
3. Return type can be the same or different (return type alone cannot
differentiate overloaded methods).
**Program**:
```java
class Calculator {
return a + b;
return a + b + c;
```
**Explanation**:
---
**Theory**:
**Program**:
```java
class Student {
String name;
int age;
Student() {
name = "Unknown";
age = 18;
Student(String name) {
this.name = name;
age = 18;
this.name = name;
this.age = age;
void display() {
```
**Explanation**:
---
**Theory**:
- **Rules**:
1. Method name, return type, and parameters must match the superclass
method.
**Program**:
```java
class Animal {
void makeSound() {
@Override
void makeSound() {
System.out.println("Dog barks");
```
**Explanation**:
- The JVM determines which method to call at runtime based on the object
type.
---
**Theory**:
Multilevel inheritance involves a chain of classes where a subclass
becomes the superclass for another class.
**Program**:
```java
class Grandparent {
void grandparentMethod() {
System.out.println("Grandparent's method");
void parentMethod() {
System.out.println("Parent's method");
void childMethod() {
System.out.println("Child's method");
```
**Explanation**:
---
**Theory**:
Java does not support multiple inheritance with classes (due to the
"diamond problem"). However, it allows a class to implement **multiple
interfaces**.
**Program**:
```java
interface Flyable {
void fly();
interface Swimmable {
void swim();
System.out.println("Duck swims");
```
**Explanation**:
---
**Theory**:
**Program**:
```java
class Animal {
void eat() {
System.out.println("Animal eats");
void bark() {
System.out.println("Dog barks");
void meow() {
System.out.println("Cat meows");
```
**Explanation**:
- Both `Dog` and `Cat` inherit `eat()` from `Animal` but have their own
unique methods.
---
**Theory**:
**Program**:
```java
String s1 = "Java";
String s3 = "Java";
```
**Explanation**:
- `s1` and `s3` point to the same string in the pool, while `s2` is a new
object.
---
**Program**:
```java
```
```java
decimal /= 2;
}
```
---
**Theory**:
**Primitive Types**:
|---------|---------|------------------|
**Program**:
```java
short s = 200;
int i = 300;
long l = 400L;
float f = 5.75f;
double d = 19.99;
char c = 'A';
```
---
**Theory**:
**Program**:
```java
class VariablesExample {
void method() {
```
**Explanation**:
---
**Theory**:
**Program**:
```java
class Parent {
void display() {
System.out.println("Parent's method");
System.out.println("Child's method");
void print() {
child.print();
```
**Output**:
```
Child's method
Parent's method
```
---
**Theory**:
**Program**:
```java
import java.util.Arrays;
s1 = s1.replaceAll("\\s", "").toLowerCase();
s2 = s2.replaceAll("\\s", "").toLowerCase();
// Check length
Arrays.sort(arr1);
Arrays.sort(arr2);
String s1 = "listen";
String s2 = "silent";
```
**Explanation**:
- Sorting both strings and comparing the sorted arrays ensures they are
anagrams.
---
---
**Theory**:
- **Steps**:
**Program**:
1. Create `mypackage/MyClass.java`:
```java
package mypackage;
System.out.println("Inside mypackage!");
```
2. Create `Main.java`:
```java
import mypackage.MyClass;
```
**Compilation**:
```
javac -d . mypackage/MyClass.java
javac Main.java
java Main
```
---
### **14. File IO Streams**
**Theory**:
**Program**:
```java
import java.io.*;
// Write to a file
} catch (IOException e) {
e.printStackTrace();
String line;
} catch (IOException e) {
e.printStackTrace();
}
```
**Explanation**:
---
**Theory**:
**Program**:
```java
import java.util.ArrayList;
// Add elements
list.add("Apple");
list.add("Banana");
// Remove elements
list.remove("Apple");
// Iterate
```
**Common Methods**:
---
**Theory**:
**Program**:
```java
// Multi-Catch
try {
System.out.println(arr[2]); // Throws
ArrayIndexOutOfBoundsException
// Nested Try
try {
try {
} catch (ArithmeticException e) {
System.out.println("Inner catch");
} catch (Exception e) {
System.out.println("Outer catch");
```
**Output**:
```
Inner catch
```
---
**Theory**:
**Program**:
```java
t1.setPriority(Thread.MIN_PRIORITY); // 1
t2.setPriority(Thread.MAX_PRIORITY); // 10
t1.start();
t2.start();
```
**Explanation**:
- Higher-priority threads get more CPU time (output order may vary).
---
### **18. Applet Lifecycle and Graphics**
**Theory**:
1. `init()`: Initialization.
**Program**:
```java
import java.applet.Applet;
import java.awt.Graphics;
```
**HTML File**:
```html
```
**Program**:
```java
import java.awt.*;
Main() {
label.setForeground(Color.RED);
add(label);
setSize(300, 200);
setVisible(true);
new Main();
```
**Explanation**:
---
### **20. Mouse and Keyboard Events**
**Program**:
```java
import java.awt.*;
import java.awt.event.*;
Main() {
addMouseListener(this);
addKeyListener(this);
setSize(300, 300);
setVisible(true);
new Main();
```
---
**Program**:
```java
import java.awt.*;
import java.awt.event.*;
Button btn;
Label result;
Main() {
btn.addActionListener(this);
setLayout(new FlowLayout());
add(tf1);
add(tf2);
add(btn);
add(result);
setSize(300, 200);
setVisible(true);
new Main();
```
**Explanation**:
---
```java
import java.awt.*;
Main() {
// Checkbox (single)
list.add("Red");
list.add("Green");
// Choice (dropdown)
choice.add("Apple");
choice.add("Banana");
add(cb1);
add(cb2);
add(male);
add(female);
add(list);
add(choice);
setLayout(new FlowLayout());
setSize(300, 300);
setVisible(true);
new Main();
```
**Explanation**:
---
This covers all questions with detailed theory and code. Let me know if
you need further clarification!