Java_Assignment_Solutions
Java_Assignment_Solutions
A class in Java is a template for creating objects. It defines fields (attributes) and methods (behaviors). An object is an
instance of a class that holds data and can perform actions.
Example:
class Student {
String name;
int age;
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
2. Multi-threading in Java
Multithreading allows multiple tasks to run concurrently, improving performance. Java provides the Thread class and
Runnable interface.
Example (Inheritance):
class Animal {
void sound() { System.out.println("Animal makes a sound"); }
}
6. Garbage Collection
Example:
class Demo {
protected void finalize() { System.out.println("Garbage Collected"); }
}