Java Programming Notes (Unit 2 to Unit 5) Exam
Ready + Programs + Syntax
UNIT 2: Inheritance, Interface, Package
Inheritance
Inheritance allows one class to acquire properties and methods of another class using 'extends'
keyword.
class Animal {
void eat() { [Link]("Eating"); }
}
class Dog extends Animal {
void bark() { [Link]("Barking"); }
}
Interface
Interface is a collection of abstract methods. It is used for multiple inheritance.
interface A {
void show();
}
class B implements A {
public void show() {
[Link]("Hello");
}
}
Package
Package is a collection of related classes and interfaces.
package mypack;
public class Test {
public void display() {
[Link]("Hello Package");
}
}
UNIT 3: Exception Handling & Multithreading
Exception Handling
Exception is a runtime error handled using try-catch blocks.
try {
int x = 10/0;
} catch(Exception e) {
[Link](e);
}
Multithreading
Thread is a lightweight process. It is created using Thread class or Runnable interface.
class MyThread extends Thread {
public void run() {
[Link]("Thread running");
}
}
UNIT 4: AWT & Swing
AWT Components
AWT is platform dependent GUI toolkit.
Frame f = new Frame();
Button b = new Button("Click");
[Link](b);
Swing Components
Swing is platform independent GUI toolkit.
JFrame f = new JFrame();
JButton b = new JButton("Click");
[Link](b);
Event Handling
Event handling is used to respond to user actions.
[Link](this);
UNIT 5: Network Programming
Socket Programming
Socket is endpoint for communication between client and server.
Socket s = new Socket("localhost",5000);
InetAddress
Used to get IP address and hostname.
InetAddress ip = [Link]("[Link]");
[Link]([Link]());
Client Server Example
ServerSocket ss = new ServerSocket(5000);
Socket s = [Link]();
URL Class
URL url = new URL("[Link]
[Link]([Link]());