Java OOP Final Exam Crash Course
1. Introduction to Object-Oriented Programming & Java Basics
OOP = Object-Oriented Programming: Code is organized using objects & classes.
Principles of OOP:
- Polymorphism: One function, many forms (e.g., draw() in Circle, Square).
- Inheritance: One class inherits from another (e.g., Dog extends Animal).
- Encapsulation: Hides internal data (private variables with public methods).
- Abstraction: Shows only essential info (e.g., using interface or abstract class).
Procedural vs OOP:
- Procedural: Step-by-step instructions.
- OOP: Uses classes and objects.
Java Terms:
- JDK: Java Development Kit (tools + compiler)
- JRE: Java Runtime Environment (to run code)
- JVM: Java Virtual Machine (executes code independently)
Data Types: int, float, char, boolean, String
Control Statements: if, switch, for, while, do-while
2. Classes, Objects, and Methods
Class = Blueprint, Object = Instance of class
Real-world: Car class -> myCar object
Constructor: Special method to initialize objects.
- Default, Parameterized, Copy Constructor
Method Overloading: Same method, different parameters.
this: Refers to current object.
static: Belongs to class, not object.
Access Modifiers: private, public, protected, default
Memory:
- Stack: local variables, method calls
- Heap: object storage
Garbage Collection: Deletes unused objects.
Object class: Base of all classes.
toString(): Converts object to string.
3. Inheritance and Polymorphism
Java OOP Final Exam Crash Course
Inheritance: Parent class -> Child class (e.g., Animal -> Dog)
Types: Single, Multilevel, Hierarchical, Hybrid (via interface)
Method Overriding: Child class changes parent method.
super: Access parent class method.
final keyword:
- final variable: constant
- final method: can't override
- final class: can't inherit
Polymorphism:
- Compile-time: Method overloading
- Runtime: Method overriding, upcasting
Abstract Class: Cannot create object, has abstract methods.
Interface: Achieve multiple inheritance.
Real-life: USB interface used by Mouse, Keyboard, etc.
4. Exception Handling
Exception: Runtime error.
try-catch: Handle exceptions
throw: Throw exception manually
throws: Declare exception
finally: Always runs
finalize(): Runs before object destroyed
Checked: IOException, SQLException
Unchecked: ArithmeticException, NullPointerException
Custom Exception: Create user-defined exceptions by extending Exception class.
5. I/O Streams and Applet
Scanner: Easy input
BufferedReader: Faster input with buffer
FileInputStream/FileOutputStream: Read/write files
Applet:
- init(), start(), paint(), stop(), destroy()
- Can display text, graphics, animations
- JApplet: Swing-based applet
Java OOP Final Exam Crash Course
Scanner vs BufferedReader:
- Scanner: Easy, slower
- BufferedReader: Fast, needs InputStreamReader