Java OOP(Object Oriented Programming) Concepts
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects
that contain data (fields) and behavior (methods). It focuses on designing software that closely
represents real-world entities. It is used to:
Improves code reusability
Enhances maintainability and scalability
Makes programs easier to understand and manage
Closely models real-world entities
Characteristics of an OOP(Object Oriented Programming)
The diagram below demonstrates the Java OOPs Concepts
Class
A Class is a user-defined blueprint or prototype from which objects are created. It represents the set
of properties or methods that are common to all objects of one type.
Using classes, you can create multiple objects with the same behavior instead of writing their code
multiple times. In general, class declarations can include these components in order:
Modifiers: A class can be public or have default access (Refer to this for details).
Class name: The class name should begin with the initial letter capitalized by convention.
Body: The class body is surrounded by braces, { }.
Example: A Car represents a class (blueprint), while BMW, Mercedes, and Audi represent objects
(instances) created from that class.
Object
An Object is a basic unit of Object-Oriented Programming that represents real-life entities. A typical
Java program creates many objects, which as you know, interact by invoking methods. The objects
are what perform your code, they are the part of your code visible to the viewer/user. An object
mainly consists of:
State: It is represented by the attributes of an object. It also reflects the properties of an
object.
Behavior: It is represented by the methods of an object. It also reflects the response of an
object to other objects.
Identity: It is a unique name given to an object that enables it to interact with other objects.
Method: A method is a collection of statements that perform some specific task and return
the result to the caller.
Example: Dog is a class, Tommy is an object of that class.
Example :
Dog d1 = new Dog(); Class Dog
↓
Dog → Class name Object Created using new keyword
d1 → Object reference ↓
new → Allocates memory Memory allocated
Dog() → Constructor call ↓
d1 points to object
Abstraction
Abstraction in Java is the process of hiding implementation details and showing only the essential
features of an object. It helps users focus on what an object does rather than how it does it.
Hides complexity: Internal implementation details are hidden from the user.
Improves maintainability: Changes in implementation do not affect the user code.
Enhances flexibility: Supports loose coupling through abstract classes and interfaces.
Example: An ATM or a coffee machine represents abstraction, where the user interacts with simple
operations while the internal working and implementation details remain hidden.
How to Achieve Abstraction
It is achieved in Java using abstract classes and interfaces.
Interfaces can provide 100% abstraction, while abstract classes provide partial abstraction.
Example: Abstract class defines a common base (Shape) using inheritance, while an interface
(Drawable) defines behavior implemented by multiple classes.