Object-Oriented Programming (OOP) in Java

Introduction to OOP Concepts

What is OOP?

Object-Oriented Programming (OOP) is a programming paradigm that organizes software using objects. Objects contain both data (attributes) and behaviour (methods).

Java is a fully object-oriented programming language that supports code reusability, modularity, and security.

Main OOP Concepts

1. Class and Object

A class is the theoretical blueprint, while an object is a concrete instance created from that blueprint.

Class: Student - String name - int rollNo + display() instantiates Object: s1 name = "Anu" rollNo = 101 display() executed

2. Encapsulation

Bundling data and methods together into a single unit (like a capsule) and keeping variables private to hide sensitive state.

Private Data (Variables) Public Methods (Getters/Setters)

3. Inheritance

The process where one class (child class) acquires the properties and behaviors of another class (parent class).

Parent: Animal eat() Child: Dog bark() + inherits eat()

4. Polymorphism

"Many forms" – The ability of a single method name to perform different actions based on the object invoking it.

makeSound() Dog Object Outputs: "Woof!" Cat Object Outputs: "Meow!"

5. Abstraction

Showing only necessary details to the user while hiding complex implementation mechanics internally.

User Interface pressStart() Hides Logic Internal Engine Complex logic & hardware control

Advantages of OOP