OOPJ
OOPJ
Example:
Java
Example:
Java
Challenges of Multithreading:
1. Increased Complexity:
o Multithreaded programs can be harder to understand, design, and maintain.
o Dealing with shared resources, synchronization, and deadlocks adds complexity
2(i) Compare and contrast the Thread class and the Runnable interface for
creating threads in Java?
1. Thread Class:
o Definition: The Thread class is a built-in class in Java that allows you to create
and manage threads.
o Usage:
▪ To create a thread by extending the Thread class, you need to create a
subclass that overrides the run() method.
▪ An instance of this subclass can then be allocated and started using
the start() method.
o Methods:
▪ The Thread class provides multiple methods, including start() (to
begin thread execution) and run() (where the actual thread logic
resides).
o Memory:
▪ Creating a thread by extending Thread requires more memory because
each thread creates a unique object associated with it.
o Limitation:
▪ Due to Java’s single inheritance constraint, if a class extends Thread, it
cannot extend any other class1.
Java
2. Runnable Interface:
o Definition: The Runnable interface represents a task that can be executed by a
thread.
o Usage:
▪ To create a thread by implementing the Runnable interface, you declare
a class that implements the run() method.
▪ An instance of this class can then be allocated, passed as an argument
when creating a Thread, and started.
o Methods:
▪ The Runnable interface has only one abstract method: run().
o Memory:
▪ Creating a thread using Runnable requires less memory because
multiple threads can share the same objects.
o Advantages:
▪ Implementing Runnable promotes better object-oriented design,
reusability, and resource sharing.
▪ It allows your class to be more flexible—you can run it in a thread, pass it
to an executor service, or use it within a single-threaded application2.
Java
while both approaches allow thread creation, implementing Runnable is generally preferred due
to its flexibility and better adherence to object-oriented principles. It allows you to separate
thread logic from the class itself, making your code more maintainable and extensible
2.(j) Explain the AWT class hierarchy in Java and discuss the role of different
container classes?
The AWT class hierarchy in Java and understand the roles of different container classes.
Role of Containers:
• Window: Represents the top-level window or dialog box. It provides a canvas for other
components and is essential for creating standalone applications.
• Panel: Acts as a lightweight container for organizing components. It doesn’t have a title
bar or border, making it suitable for grouping related elements.
• Frame: A heavyweight container with a title bar and border. It’s commonly used for
building application windows.
• Dialog: Used for temporary interactions with the user, such as input prompts or alerts.
the AWT class hierarchy includes components, containers, and layout managers, allowing
developers to create platform-independent GUIs in Java
2.(k) Write a Java program that demonstrates the use of adapter classes in event
handling?
Adapter classes in Java provide default implementations for listener interfaces, allowing you to
avoid implementing all the methods of those interfaces. They are particularly useful when you
want to work with unrelated classes or need to increase class transparency and reusability1.
Let’s create two examples—one using the WindowAdapter class and another using
the MouseAdapter class:
1. Java WindowAdapter Example:
o In this example, we’ll use the WindowAdapter class to handle window events.
Specifically, we’ll override the windowClosing() method to close the frame
window when the user clicks the close button.
Java
import java.awt.*;
import java.awt.event.*;
public AdapterExample() {
Frame f = new Frame("Window Adapter");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
f.dispose(); // Close the frame
}
});
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}
}
Output: When you run this program, a frame titled “Window Adapter” will appear. When
you click the close button, the frame will be disposed of and closed.
2. Java MouseAdapter Example:
o In this example, we’ll use the MouseAdapter class to handle mouse events. We’ll
override the mouseClicked() method to display a message when the user clicks
the mouse inside the frame.
Java
import java.awt.*;
import java.awt.event.*;
public MouseAdapterExample() {
Frame f = new Frame("Mouse Adapter");
f.addMouseListener(this); // Add MouseListener to the frame
f.setSize(300, 300);
f.setLayout(null);
f.setVisible(true);
}
Output: When you run this program, a frame titled “Mouse Adapter” will appear. Click
anywhere inside the frame, and it will display the coordinates of the mouse click.
2.(l) Write a simple Swing program that includes a JButton, a JLabel. and a
JTextField?
simple Java Swing program that includes a JButton, a JLabel, and a JTextField. The
program creates a basic window with these components:
Java
import javax.swing.*;
import java.awt.*;
// Create a JLabel
JLabel label = new JLabel("Enter your name:");
label.setBounds(20, 20, 100, 20);
// Create a JTextField
JTextField textField = new JTextField();
textField.setBounds(130, 20, 150, 20);
// Create a JButton
JButton button = new JButton("Submit");
button.setBounds(130, 60, 80, 30);
In this program:
Let’s create a simple Java class called Person that includes constructors with different
parameters and methods to perform user-specific tasks. In this example, we’ll define
constructors for setting the person’s name and age, as well as a method to display their
information.
Java
class Person {
private String name;
private int age;
// Display information
person1.displayInfo();
person2.displayInfo();
person3.displayInfo();
}
}
In this example:
• We define three constructors with different parameters (no arguments, name, and name
+ age).
• The displayInfo() method displays the person’s details.
• You can create Person objects using any of the constructors based on your
requirements.
Q.4 Write Java code that demonstrates the usage of method overloading,
method overriding, and dynamic method dispatch?
1. Method Overloading:
o Method overloading allows defining multiple methods with the same name in a
class, but with different parameters (number or types of parameters).
o The compiler determines which method to call based on the arguments provided
during the method invocation.
o Example of method overloading:
Java
class Calculator {
public int add(int num1, int num2) {
return num1 + num2;
}
2. Method Overriding:
o Method overriding occurs when a subclass provides a specific implementation
for a method that is already defined in its superclass.
o The method signature (name and parameters) must be the same in both the
superclass and the subclass.
o Example of method overriding:
Java
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
Java
class Shape {
void display() {
System.out.println("I am in the Shape class");
}
}
Output:
I am in the Rectangle class
I am in the Triangle class
Method overloading allows defining multiple methods with the same name but different
parameters, method overriding provides specific implementations in subclasses, and
dynamic method dispatch enables runtime polymorphism by determining the method to
execute based on the actual object type.
Q.5 Implement a comprehensive Java application that utilizes AWT controls to create
import java.awt.*;
import java.awt.event.*;
// Create components
Label label = new Label("Enter your name:");
TextField textField = new TextField();
Button submitButton = new Button("Submit");
• We create a frame (window) with a label, a text field, a submit button, and a menu bar.
• The submit button has an action listener that responds when clicked.
• You can further enhance this UI by adding more components and functionality.