OOP Java Detailed Guide CodeWithGauravSKulkarni
OOP Java Detailed Guide CodeWithGauravSKulkarni
Table of Contents
● 1. What is OOP?
● 2. Benefits of OOP
● 3. Java OOP Features
● 4. Class & Object
● 5. Abstraction
● 6. Encapsulation
● 7. Inheritance
● 8. Polymorphism
● 9. Hands On In Java
–CodeWithGauravSKulkarni
What is OOP?
● Definition:
– As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in
programming.
○ A programming paradigm based on objects and classes, enabling modular, reusable, and maintainable code.
→ OOPs Concepts:
● Class
● Objects
● Data Abstraction
● Encapsulation
● Inheritance
● Polymorphism
● Dynamic Binding
● Message Passing
Real-World Examples:
Modeling real-world entities like Car, BankAccount
–CodeWithGauravSKulkarni
Benefits of OOP
1. Modularity
Code is organized into classes — makes it easier to manage, debug, and maintain.
2. Reusability (DRY)
Use inheritance to reuse code across multiple classes. Write once, use everywhere.
3. Encapsulation
Keep data private inside objects and expose only what’s needed — protects against accidental modification.
4. Abstraction
Hide complex implementation details — focus on what the object does, not how.
5. Polymorphism
Same method name, different behaviors — allows flexibility and cleaner interfaces.
6. Scalability
Easier to extend and scale applications without breaking existing code.
7. Real-world modeling
You can structure your app similar to real-life entities (like Car, User, Order) — intuitive design.
8. Maintainability
Smaller, isolated components mean fewer bugs and easier refactoring.
●
–CodeWithGauravSKulkarni
Class
● Definition:
○ A class is a user-defined data type.
○ It consists of data members and member functions, which can be accessed and used by creating an
instance of that class. It represents the set of properties or methods that are common to all objects of
one type.
○ In other words we can say that , class is like a blueprint for an object.
Example :
1: let’s take example of car car is the class , and tata safari , mahindra suv 700 , creata are objects of the class .
here may be many cars with different names and brands but all of them will share some common properties like
all of them will have 4 wheels, Speed Limit, Mileage range, etc.
–CodeWithGauravSKulkarni
Object
● Definition:
It is a basic unit of Object-Oriented Programming and represents the real-life entities. An Object is
an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated
(i.e. an object is created) memory is allocated. An object has an identity, state, and behavior. Each
object contains data and code to manipulate the data. Objects can interact without having to know
details of each other’s data or code, it is sufficient to know the type of message accepted and type
of response returned by the objects.
For example “Dog” is a real-life Object, which has some characteristics like color, Breed, Bark,
Sleep, and Eats.
–CodeWithGauravSKulkarni
Abstraction
● Definition:
○ Data abstraction is one of the most essential and important features of object-oriented
programming. Data abstraction refers to providing only essential information about the data to
the outside world, hiding the background details or implementation.
-> Consider a real-life example of a man driving a car. The man only knows that pressing the
accelerators will increase the speed of the car or applying brakes will stop the car, but he does not know
about internal mechanism
–CodeWithGauravSKulkarni
Encapsulation
● Definition:
○ Encapsulation bundles data and methods, restricting direct access using access modifiers.
○ Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism
that binds together code and the data it manipulates. In Encapsulation, the variables or data of
a class are hidden from any other class and can be accessed only through any member function
of their class in which they are declared. As in encapsulation, the data in a class is hidden from
other classes, so it is also known as data-hiding.
–CodeWithGauravSKulkarni
Inheritance
● Definition:
○ Inheritance is an important pillar of OOP(Object-Oriented Programming). The capability of a class to derive
properties and characteristics from another class is called Inheritance.
○ Inheritance lets a subclass inherit attributes and methods from a superclass, promoting code reuse.
○ When we write a class, we inherit properties from other classes. So when we create a class, we do not need to
write all the properties and functions again and again, as these can be inherited from another class that
possesses it. Inheritance allows the user to reuse the code whenever possible and reduce its redundancy.
Polymorphism
● Definition:
○ The word polymorphism means having many forms.
○ Polymorphism in Java refers to the ability of an object to take on many forms. It allows a single
action to be performed in different ways. This is achieved through method overloading
(compile-time polymorphism) and method overriding (runtime polymorphism)
○ Polymorphism allows objects to be treated as instances of their parent class, supporting overloading and
overriding.
–CodeWithGauravSKulkarni
Creating Class
String name;
int age;
this.name = name;
this.age = age;
}
public void bark() {