0% found this document useful (0 votes)
3 views

OOP Java Detailed Guide CodeWithGauravSKulkarni

The document provides an overview of Object-Oriented Programming (OOP) concepts in Java, including definitions and benefits of OOP, such as modularity, reusability, and encapsulation. It explains key OOP features like classes, objects, inheritance, abstraction, encapsulation, and polymorphism, along with real-world examples. Additionally, it includes a sample code demonstrating class creation and object instantiation in Java.

Uploaded by

Gaurav Kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

OOP Java Detailed Guide CodeWithGauravSKulkarni

The document provides an overview of Object-Oriented Programming (OOP) concepts in Java, including definitions and benefits of OOP, such as modularity, reusability, and encapsulation. It explains key OOP features like classes, objects, inheritance, abstraction, encapsulation, and polymorphism, along with real-world examples. Additionally, it includes a sample code demonstrating class creation and object instantiation in Java.

Uploaded by

Gaurav Kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

–CodeWithGauravSKulkarni

OOP Concepts in Java


–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

Benefits of OOP (Object-Oriented Programming)

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

OOP With Java


–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.

There are several types of inheritance:

● Single Inheritance: A class inherits from only one superclass.


● Multilevel Inheritance: A class inherits from another class, which in turn inherits from another class, forming a chain of inheritance.
● Hierarchical Inheritance: Multiple classes inherit from a single superclass.
● Multiple Inheritance: A class inherits from multiple superclasses. Java doesn't directly support multiple inheritance with classes, but
it can be achieved through interfaces.
● Hybrid Inheritance: A combination of two or more types of inheritance, often involving interfaces to achieve multiple inheritance-like
behavior.
–CodeWithGauravSKulkarni

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

1: public class Main {


int x = 5;
}

2:public class Dog {

String name;

int age;

public Dog(String name, int age) {

this.name = name;

this.age = age;

}
public void bark() {

System.out.println(name + " says Woof!");

public static void main(String[] args) {

Dog dog1 = new Dog("Buddy", 3);

Dog dog2 = new Dog("Max", 5);

System.out.println(dog1.name + " is " + dog1.age + "


years old.");

dog1.bark(); // Output: Buddy says Woof!

System.out.println(dog2.name + " is " + dog2.age + "


years old.");

dog2.bark(); // Output: Max says Woof!

You might also like