1.
Define Inheritance:
Inheritance is an object-oriented programming feature where one class (child) can inherit the
properties and methods of another class (parent), promoting code reuse and hierarchy.
2. What are the Types of Inheritance?
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance (via interfaces in Java)
- Hybrid Inheritance
3. How to define an Interface?
In Java, an interface is defined using the "interface" keyword:
interface Drawable {
void draw();
4. Difference between Interface and Abstract Classes:
- Interface: Only abstract methods (Java 8+ allows default/static methods). No constructor.
- Abstract Class: Can have both abstract and non-abstract methods. Can have constructors and
variables.
5. Define Exception Handling Techniques with Example:
Exception handling manages runtime errors using try, catch, finally, and throw:
try {
int a = 5/0;
} catch (ArithmeticException e) {
[Link]("Cannot divide by zero");
}
6. What is the Main Thread with Example:
The main thread is the first thread started when a Java program begins:
public class Main {
public static void main(String[] args) {
[Link]([Link]().getName());
7. Characteristics of the Main Thread:
- It starts automatically in every Java program.
- It has the highest priority by default (priority 5).
- It runs until all non-daemon threads finish.
8. Comparison of Methods in Creation of New Threads:
- Extending Thread class: class MyThread extends Thread
- Implementing Runnable: class MyThread implements Runnable
Runnable is preferred for flexibility and multiple inheritance.
9. Range of Thread Priority:
Java thread priorities range from 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY), with 5
(NORM_PRIORITY) as default.
10. What are Multithreading and its Benefits?
Multithreading is the ability of a CPU to execute multiple threads simultaneously.
Benefits:
- Better CPU utilization
- Faster execution
- Resource sharing
- Responsive UI in GUI apps