Java Object-Oriented Programming (OOP)
- Java Object-Oriented Programming (OOP) is a programming paradigm that’s based on the concept of “objects”.
- These objects can speak to real-world entities and are characterized by their properties (attributes) and behaviors (methods).
- Java, as an object-oriented programming(OOP) language, takes after the OOP paradigm and permits developers to organize software in terms of objects, which makes it less demanding to manage complex systems.
There are four primary principles in OOPs that form the establishment of Java object-oriented programming (OOP) design:

- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Object:
- Object is an entity which is having state and behaviour.
- State is nothing but characteristics of an object.
- Behaviour is nothing but functionality of an object.
- To represent state of an object, we make use of non-static data members.
- To represent behaviour of an object, we make use of non-static member function.
Class:
- In Java, a class could be a blueprint or layout for creating objects.
- It characterizes the properties (attributes or fields) and behaviors (methods or functions) that the objects made from the class will have.
- A class encapsulates information for the object and characterizes methods to control that information.
1. Encapsulation
- Encapsulation is the concept of wrapping or bundling the information (variables) and the methods (functions) that work on the information into a single unit, i.e., a class.
- It is used to hide the internal workings of an object and expose only necessary information and behavior.
Why encapsulate?
- Protects object integrity by restricting access to certain parts of an object.
- Provides data hiding, which prevents direct access to the fields (variables) and only allows access through methods (getters/setters).
Real-time Example:
- Think of a bank account. The account balance may be a private piece of information that ought to not be specifically modified by outside clients. Instep, the bank gives open methods such as deposit() and withdraw() to modify the balance whereas guaranteeing certain rules are taken after (e.g., you cannot withdraw more cash than the account balance).
2. Abstraction
- Abstraction is the concept of hiding the execution details and appearing as it were the fundamental features of an object.
- It permits you to focus on what an object does instead of how it does it.
In Java, abstraction can be achieved using:
- Abstract Classes
- Interfaces
Real-time Example:
- Think of a TV remote control. You do not got to know the inner workings of the TV to utilize the farther; you just have to be press buttons like “Power On”, “Volume Up”, and “Channel Change.” The complex points of interest of how the TV works inside are abstracted away from you.
3. Inheritance
Inheritance permits a new class (child class or subclass) to acquire properties and behaviors (fields and methods) from an existing class (parent class or superclass). This makes a difference in code reusability and builds up a relationship between the parent and child classes.
Real-time Example:
- Consider a company hierarchy. A general class Employee can have common properties like name, id, and salary. Particular types of employees like Manager or Developer can acquire from Employee and include their own particular properties and behaviors.
4. Polymorphism
Polymorphism is the capacity of an object to require on numerous forms. It permits one entity (like a method or class) to be utilized completely different ways.
Real-time Example:
- Imagine a payment processing system. Whether the payment is made through CreditCard, DebitCard, or PayPal, you’ll call the same method processPayment(), and each payment method will handle it in an unexpected way.
There are two types of polymorphism:
- Compile-time polymorphism (Method Overloading)
- Runtime polymorphism (Method Overriding)
4.1 Method Overloading (Compile-time Polymorphism)
Method overloading is when multiple methods with the same name exist in the same class, but with different parameter types or numbers.
4.2 Method Overriding (Runtime Polymorphism)
Method overriding allows a subclass to provide a specific implementation of a method already defined in its superclass.
Summary of Java Object-Oriented Programming (OOP) Concepts in Java:
- Encapsulation: Bundling information and methods that work on that information inside a class and limiting direct access to the class’s variables.
- Abstraction: Hiding the execution details and exposing as it were the essential features (accomplished with abstract classes and interfaces).
- Inheritance: A subclass can acquire properties and behaviors from a superclass, advancing code reusability and building up hierarchical relationships.
- Polymorphism: An entity (method or class) can take on numerous forms. It empowers method overloading (compile-time) and method overriding (runtime).
These four core principles help Java developers to type in cleaner, modular, and viable code.