DEV Community

Divya Divya
Divya Divya

Posted on

Java OOPs?

OOP in Java (Object-Oriented Programming) is a programming approach where you design programs using objects and classes instead of just functions and logic.

  1. Class
  2. Object
  3. Encapsulation
  4. Inheritance
  5. Polymorphism
  6. Abstraction

Class:
A class is a blueprint or template used to create objects.
It defines variables (data) and methods (behavior).

Example:

“Car” is a class — it defines properties like color, speed, and behavior like drive.

Object:
An object is an instance of a class.
It represents a real-world entity and contains data + behavior.

Key Points:

  • Object occupies memory
  • Used to access class members
  • Can create multiple objects from one class

Example :
Class → Car
Object → My car

Explanation:
Properties → color, model, speed
Behaviors → drive, brake

So, a specific car (like your red car) is an object.

Encapsulation:
Encapsulation is the process of wrapping data (variables) and methods into a single unit (class) and restricting direct access to the data.

Example:
Bank account: You cannot directly access balance; you use deposit/withdraw methods.

Inheritance:
Inheritance is a concept where one class (child) acquires properties and methods of another class (parent).

  • Parent class (Superclass) → common features

  • Child class (Subclass) → inherits + can add new features

Example:

Dog inherits from Animal → Dog can eat (common) and also bark (its own).

Polymorphism:
Polymorphism means “many forms” — the same method or function can behave differently in different situations.

Example:

A person speaks:

Teacher → teaches
Student → learns

Same action “speak”, different behavior.

Abstraction:
Abstraction means hiding internal implementation details and showing only the essential features to the user.

Example:

ATM machine: You withdraw money without knowing internal banking process.

Top comments (0)