Home Ultimate OOPS Cheat Sheet for Last-Minute Exam Prep ๐Ÿš€
Post
Cancel

Ultimate OOPS Cheat Sheet for Last-Minute Exam Prep ๐Ÿš€

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

AlgorithmTime Complexity (Average)Key Point
Linear SearchO(n)Simple, sequential check. Works on any list.
Binary SearchO(log n)Requires the array to be sorted. Very fast.
Selection SortO(nยฒ)Finds the smallest element and swaps it to the front.
Insertion SortO(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 Exception class.
    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:
    1. extends Thread: class MyThread extends Thread { ... }
    2. 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.
This post is licensed under CC BY 4.0 by the author.

How I Hacked My First Hackathon: ARIFA Hackathon 2025 Journey ๐Ÿš€๐Ÿ”ฅ

-