Object-Oriented Programming (OOP) in Java: Class Notes
1. Introduction to OOP
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects",
which can contain data and code. Data is in the form of fields (also known as attributes),
and code is in the form of procedures (methods).
2. Four Pillars of OOP
- Encapsulation: Wrapping data (variables) and code (methods) together as a single unit. Access
modifiers help achieve it.
- Abstraction: Hiding complex implementation details and showing only the necessary features.
Achieved using abstract classes and interfaces.
- Inheritance: One class inherits properties and behaviors from another class.
- Polymorphism: One interface, many implementations. Includes:
- Compile-time (method overloading)
- Run-time (method overriding)
3. Key OOP Concepts in Java
- Class: Blueprint for creating objects.
class Car {
String color;
void drive() {
System.out.println("Car is driving");
}
- Object: Instance of a class.
Car myCar = new Car();
myCar.drive();
- Constructor:
Car(String c) {
color = c;
- Access Modifiers: public, private, protected, default
- this keyword: Refers to the current object.
- super keyword: Refers to the immediate parent class.
4. Inheritance Example
class Animal {
void sound() {
System.out.println("Animal sound");
class Dog extends Animal {
void sound() {
System.out.println("Bark");
5. Abstraction Example
abstract class Shape {
abstract void draw();
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
6. Interface Example
interface Drawable {
void draw();
class Rectangle implements Drawable {
public void draw() {
System.out.println("Drawing Rectangle");
7. Polymorphism Example
Shape s = new Circle(); // run-time polymorphism
s.draw();
8. Final Keyword
- final class: Cannot be extended.
- final method: Cannot be overridden.
- final variable: Value cannot be changed.
9. Static Keyword
Used for memory management. Static members belong to the class, not to instances.
Conclusion:
OOP in Java allows better code organization, reuse, and maintainability by modeling real-world
entities into classes and objects.