OOP Core Concepts
- Abstract Class vs. Interface
Abstract Class: Use when objects are closely related (“is-a” relationship). A class can extend only one. Can have implemented methods, instance variables, and constructors.
Interface: Use for defining a capability for unrelated classes (“can-do” relationship). A class can implement multiple. All variables are public static final. Can have default methods.
Inheritance: A class acquires the fields and methods of another.
Syntax: class Child extends Parent { … }
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:```java
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!”);
}
1 |
|
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 call run() 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 finally block 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 the Stage.
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.