OOP Core Concepts
- Abstract Class vs. Interface
- Abstract Class: Use when objects are closely related (โis-aโ relationship). A class can
extendonly one. Can have implemented methods, instance variables, and constructors. - Interface: Use for defining a capability for unrelated classes (โcan-doโ relationship). A class can
implementmultiple. All variables arepublic static final. Can havedefaultmethods.
- Abstract Class: Use when objects are closely related (โis-aโ relationship). A class can
- Inheritance: A class acquires the fields and methods of another.
- Syntax:
class Child extends Parent { ... }
- Syntax:
Data Structures
- Stack (LIFO: Last-In, First-Out)
push(): Adds an element to the top.pop(): Removes an element from the top.- Stack Underflow: Error from calling
pop()on an empty stack. - Stack Overflow: Error from calling
push()on a full stack.
- Queue (FIFO: First-In, First-Out)
enqueue(): Adds an element to the rear.dequeue(): Removes an element from the front.
Searching & Sorting Algorithms
| Algorithm | Time Complexity (Average) | Key Point |
|---|---|---|
| Linear Search | O(n) | Simple, sequential check. Works on any list. |
| Binary Search | O(log n) | Requires the array to be sorted. Very fast. |
| Selection Sort | O(nยฒ) | Finds the smallest element and swaps it to the front. |
| Insertion Sort | O(nยฒ) | Builds the final sorted array one item at a time. |
Exception Handling
- Purpose: To handle runtime errors gracefully without crashing.
- Syntax:
1 2 3 4 5 6 7
try { // Code that might fail int result = 10 / 0; } catch (ArithmeticException e) { // Code to run if the error happens System.out.println("Cannot divide by zero!"); }
- User-Defined Exception: Create your own exception by extending the
Exceptionclass.1 2 3 4 5 6 7
class InvalidAmountException extends Exception { public InvalidAmountException(String message) { super(message); } } // Use it: throw new InvalidAmountException("Amount cannot be negative.");
Multithreading
- Purpose: To run multiple tasks concurrently.
- How to Create:
extends Thread:class MyThread extends Thread { ... }implements Runnable:class MyTask implements Runnable { ... }(This is usually preferred).
- Key Methods:
run(): The code that the thread will execute.start(): To begin the threadโs execution. Never callrun()directly.Thread.sleep(milliseconds): To pause a thread.
File I/O (in java.io)
File: Represents a file path.File myFile = new File("data.txt");FileOutputStream: Writes raw bytes to a file.out.write(myString.getBytes());
FileInputStream: Reads raw bytes from a file.int byteData = in.read();(returns -1 at end of file)
- Crucial: Always close your streams in a
finallyblock or use a try-with-resources statement to prevent resource leaks.myStream.close();
JavaFX & Swing (GUI)
- Core Idea: Build UIs with components and respond to user actions (events).
- JavaFX Basics:
Stage: The main application window.Scene: The content inside theStage.- Layouts: Arrange components (e.g.,
GridPane,VBox,HBox). - Controls: Buttons, Labels, TextFields, etc.
- Event Handling:
myButton.setOnAction(e -> { /* what to do on click */ });
- Swing Basics:
JFrame: The main window.JPanel: A container to hold other components.- Layouts:
BorderLayout,GridLayout, etc. - Components:
JButton,JLabel,JTextField, etc. - Event Handling: Use
ActionListener.