✍️Object-Oriented Programming (OOP)
in Java – Handwritten Style Notes
Introduction to OOP and Java Basics
Object-Oriented Programming (OOP) is a programming paradigm based on the concept
of "objects", which contain data and methods.
The four main principles of OOP are:
1. Abstraction - Hiding complex implementation details and showing only essential
features.
2. Encapsulation - Wrapping data and methods into a single unit (class) and restricting
access.
3. Inheritance - Acquiring properties and behaviors of one class in another class.
4. Polymorphism - Performing a single action in different ways (method overloading and
overriding).
Java is a high-level, platform-independent, object-oriented language developed by Sun
Microsystems. It runs on the Java Virtual Machine (JVM).
Java applications are "write once, run anywhere". The Java Development Kit (JDK)
provides tools to write and run Java programs.
Classes and Objects
A class is a blueprint for creating objects. It contains variables (fields) and methods.
Syntax:
class ClassName {
int variable;
void method() {
// code
}
}
Object: An instance of a class.
Creating an object:
ClassName obj = new ClassName();
Constructors: Special methods used to initialize objects. They have the same name as
the class.
- Default constructor (no parameters)
- Parameterized constructor (with parameters)
The 'this' keyword refers to the current class object.
Static keyword defines class-level variables and methods (shared by all objects).
Method overloading: Multiple methods with the same name but different parameters.
Inheritance and Interfaces
Inheritance allows one class to inherit features from another class.
Syntax:
class Child extends Parent { }
Types of inheritance:
- Single
- Multilevel
- Hierarchical
The 'super' keyword refers to the immediate parent class.
Method overriding: Redefining a method in the subclass.
Abstract class: Contains abstract methods (no body) and cannot be instantiated.
Interface: A completely abstract class with only method signatures.
Syntax:
interface InterfaceName {
void method();
}
class ClassName implements InterfaceName { }
Polymorphism and Encapsulation
Polymorphism allows performing one task in many ways.
Compile-time polymorphism: Method overloading.
Runtime polymorphism: Method overriding.
The 'final' keyword is used to declare constants, prevent inheritance, and method
overriding.
Encapsulation means binding the data and the code that manipulates it together.
Use private variables and public getter/setter methods to achieve encapsulation.
Access Modifiers:
- private: accessible within the class
- public: accessible everywhere
- protected: accessible in the same package and subclasses
- default: accessible within the same package
Packages: Groups related classes and interfaces together.
Exception Handling and File I/O
Exceptions are unwanted or unexpected events that occur during program execution.
Types:
- Checked (compile-time)
- Unchecked (runtime)
Syntax:
try {
// code
} catch(Exception e) {
// handle error
} finally {
// always executes
}
'throw' is used to explicitly throw an exception.
'throws' is used in method declaration.
File Handling: Reading and writing files using FileReader, FileWriter, BufferedReader,
etc.
Example:
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
Multithreading and Collections
Multithreading is the process of executing multiple threads simultaneously.
Creating a thread:
- By extending Thread class
- By implementing Runnable interface
Thread lifecycle: New, Runnable, Running, Blocked, Terminated
Synchronization: Used to control access of multiple threads to shared resources.
Collections Framework: Provides classes and interfaces for storing and manipulating
groups of data.
List: Ordered collection (e.g., ArrayList)
Set: No duplicates (e.g., HashSet)
Map: Key-value pairs (e.g., HashMap)