## 1.
Classes and Objects
**Code:**
```java
class Car {
String color;
int speed;
void display() {
System.out.println("Color: " + color + ", Speed: " + speed);
}
public static void main(String[] args) {
Car c1 = new Car();
c1.color = "Red";
c1.speed = 120;
c1.display();
}
}
Output:
Color: Red, Speed: 120
2. Methods
Code:
Java
class Calculator {
int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("Sum: " + calc.add(10, 20));
}
}
Output:
Sum: 30
3. Constructors and Its Types
Code:
Java
class Student {
String name;
// Default Constructor
Student() {
name = "Unknown";
}
// Parameterized Constructor
Student(String n) {
name = n;
}
void show() {
System.out.println("Name: " + name);
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("Alice");
s1.show();
s2.show();
}
}
Output:
Name: Unknown
Name: Alice
4. Method Overloading
Code:
Java
class Overload {
void show(int a) {
System.out.println("int: " + a);
}
void show(String b) {
System.out.println("String: " + b);
}
public static void main(String[] args) {
Overload obj = new Overload();
obj.show(5);
obj.show("Hello");
}
}
Output:
int: 5
String: Hello
5. Inheritance and Its Types
Code:
Java
// Single Inheritance
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}
Output:
Eating...
Barking...
6. Polymorphism and Its Types
Code:
Java
// Compile-time Polymorphism
class Poly {
void greet() {
System.out.println("Hello");
}
void greet(String name) {
System.out.println("Hello, " + name);
}
public static void main(String[] args) {
Poly p = new Poly();
p.greet();
p.greet("Alice");
}
}
Output:
Hello
Hello, Alice
7. Method Overriding
Code:
Java
class Vehicle {
void run() {
System.out.println("Vehicle is running");
}
}
class Bike extends Vehicle {
void run() {
System.out.println("Bike is running safely");
}
public static void main(String[] args) {
Vehicle obj = new Bike();
obj.run();
}
}
Output:
Bike is running safely
8. Access Modifiers
Code:
Java
class AccessDemo {
public int pub = 1;
private int pri = 2;
protected int pro = 3;
int def = 4;
void show() {
System.out.println(pub + ", " + pri + ", " + pro + ", " + def);
}
public static void main(String[] args) {
AccessDemo obj = new AccessDemo();
obj.show();
}
}
Output:
1, 2, 3, 4
9. Types of Variables
Code:
Java
class Variables {
static int staticVar = 100;
int instanceVar = 200;
void method() {
int localVar = 300;
System.out.println(staticVar + ", " + instanceVar + ", " + localVar);
}
public static void main(String[] args) {
new Variables().method();
}
}
Output:
100, 200, 300
10. Scope of Variables
Code:
Java
class Scope {
int x = 5;
void test() {
int x = 10;
System.out.println("Local: " + x);
System.out.println("Instance: " + this.x);
}
public static void main(String[] args) {
new Scope().test();
}
}
Output:
Local: 10
Instance: 5
11. Abstract Classes
Code:
Java
abstract class Shape {
abstract void draw();
}
class Rectangle extends Shape {
void draw() {
System.out.println("Drawing Rectangle");
}
public static void main(String[] args) {
Shape s = new Rectangle();
s.draw();
}
}
Output:
Drawing Rectangle
12. Interfaces
Code:
Java
interface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() {
System.out.println("Drawing Circle");
}
public static void main(String[] args) {
Drawable d = new Circle();
d.draw();
}
}
Output:
Drawing Circle
13. Multiple Inheritance using Interface
Code:
Java
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
public void methodA() {
System.out.println("Method A");
}
public void methodB() {
System.out.println("Method B");
}
public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}
Output:
Method A
Method B
14. Encapsulation
Code:
Java
class Person {
private String name;
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
public static void main(String[] args) {
Person p = new Person();
p.setName("Ravi");
System.out.println(p.getName());
}
}
Output:
Ravi
15. AWT and Its Components
Code:
Java
import java.awt.*;
public class MyAWT {
public static void main(String[] args) {
Frame f = new Frame("AWT Example");
Button b = new Button("Click Me");
b.setBounds(60, 100, 80, 30);
f.add(b);
f.setSize(250, 200);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
(A simple window titled "AWT Example" with a button labeled "Click Me" will appear.)
16. Event Handling
Code:
Java
import java.awt.*;
import java.awt.event.*;
public class EventDemo {
public static void main(String[] args) {
Frame f = new Frame("Event Example");
Button b = new Button("Click");
b.setBounds(100, 100, 80, 30);
b.addActionListener(e -> System.out.println("Button Clicked"));
f.add(b);
f.setSize(300, 200);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
(Output in the console, each time the button is clicked:
Button Clicked
Button Clicked
...)
17. Event Delegation Model
Code:
Java
import java.awt.*;
import java.awt.event.*;
class EventDelegation implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Delegated event handled");
}
public static void main(String[] args) {
Frame f = new Frame("Delegation");
Button b = new Button("Click");
b.setBounds(100, 100, 80, 30);
EventDelegation ed = new EventDelegation();
b.addActionListener(ed);
f.add(b);
f.setSize(300, 200);
f.setLayout(null);
f.setVisible(true);
}
}
Output:
(Output in the console, each time the button is clicked:
Delegated event handled
Delegated event handled
...)
18. Threads
Code:
Java
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++)
System.out.println("Thread running: " + i);
}
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
}
}
Output:
(Order may vary)
Thread running: 1
Thread running: 2
Thread running: 3
Thread running: 4
Thread running: 5
19. Thread Life Cycle
Code:
Java
class LifeCycle extends Thread {
public void run() {
System.out.println("Thread is running...");
}
public static void main(String[] args) {
LifeCycle t = new LifeCycle();
System.out.println("State: " + t.getState());
t.start();
System.out.println("State after start: " + t.getState());
}
}
Output:
State: NEW
Thread is running...
State after start: RUNNABLE
20. Creating Threads (Runnable Interface)
Code:
Java
class MyRunnable implements Runnable {
public void run() {
for (int i = 1; i <= 3; i++)
System.out.println("Runnable thread: " + i);
}
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable());
t.start();
}
}
Output:
(Order may vary)
Runnable thread: 1
Runnable thread: 2
Runnable thread: 3