Overview of Object-Oriented Programming (OOP) Upto Encapsulation
Overview of Object-Oriented Programming (OOP) Upto Encapsulation
(OOP)
Definition: Object-Oriented Programming (OOP) is a
programming paradigm that organizes software design around
data, or objects, rather than functions and logic. Objects are
instances of classes and encapsulate both data and methods that
operate on that data.
• Modularity: Divides a program into distinct objects, each with its own
responsibility, making it easier to understand and manage.
• Reusability: Objects and classes can be reused across different programs
and projects, reducing redundancy.
• Maintainability: Encapsulation and abstraction help in managing
complexity and making code easier to modify and extend.
• Flexibility and Extensibility: Polymorphism and inheritance allow for
more flexible and easily extensible code structures.
Features of Object Oriented Programming
What is a Class?
Components of a Class
• Attributes (Fields): Variables that hold data specific to each object created
from the class.
• Methods: Functions defined within a class that describe the behaviors or
actions an object can perform.
Example
// Constructor
public ClassName(parameters) {
// Initialization code
}
// Methods
returnType methodName(parameters) {
// Method body
}
}
// Define the Car class
class Car {
// Attributes (fields) with default access
String make;
int year;
– Car(String make, int year) - Initializes the make and year attributes
with the values provided when a Car object is created.
• Method:
– void displayInfo() - Prints out the details of the car to the console.
What is an Object?
Definition
Creating an Object
To create an object, you instantiate the class using the new keyword followed
by the class constructor.
we’ll create a Car object in the main method and use it to call the display Info method.
myCar.displayInfo();
}
Explanation of the Main Class
• Creating an Object:
When you run the Main class, the displayInfo method of the myCar object will
print the details of the car to the console. Here’s what the output will look
like: