0% found this document useful (0 votes)
21 views12 pages

Overview of Object-Oriented Programming (OOP) Upto Encapsulation

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

Overview of Object-Oriented Programming (OOP) Upto Encapsulation

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

Overview of Object-Oriented Programming

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

Purpose: OOP aims to enhance software modularity, reusability,


and maintainability by organizing complex systems into more
manageable and logically grouped components.
Benefits of OOP

• 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?

Class: A class is a blueprint or template for creating objects. It defines a data


structure by bundling data (attributes) and methods (functions) that operate
on the data.

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

public class ClassName {


// Fields (Attributes)
dataType fieldName;

// 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;

// Constructor to initialize the Car object


Car(String make, int year) {
this.make = make;
this.year = year;
}

// Method to display car details


void displayInfo() {
System.out.println("Car Make: " + make);
System.out.println("Car Year: " + year);
}
}
• Attributes:

– String make; - Stores the make (brand) of the car.

– int year; - Stores the manufacturing year of the car.

– Attributes are not marked private, so they have default (package-


private) access and can be accessed directly by any other class in the
same package.
• Constructor:

– 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

Object: An object is an instance of a class. It is a self-contained unit that


combines both data and methods defined by the class. Objects represent
real-world entities or concepts in a program.

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.

public class Main {

public static void main(String[] args) {

// Create a Car object

Car myCar = new Car("Toyota", 2021);

// Call the displayInfo method on the Car object

myCar.displayInfo();

}
Explanation of the Main Class
• Creating an Object:

– Car myCar = new Car("Toyota", 2021); - This line creates an instance


of the Car class named myCar. The Car constructor initializes make to
"Toyota" and year to 2021.
• Calling a Method:

– myCar.displayInfo(); - Calls the displayInfo method on the myCar


object to print out the car's make and year.
Expected Output

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:

Car Make: Toyota

Car Year: 2021


Encapsulation

• Definition: Encapsulation is the principle of bundling data (attributes) and


methods (functions) that operate on the data into a single unit, called a
class. It restricts direct access to some of an object's components and
prevents unintended interference and misuse.
• Benefits:

– Data Hiding: Internal state of an object is protected from external


access and modification.
– Controlled Access: Access to the object's data is controlled through
public methods (getters and setters).
// Define the Person class with default access
class Person { // Method to get the current age
// Attributes (fields) with default access int getAge() {
String name; return age;
int age; }
}
// Constructor to initialize the Person object // Define the Main class to run the program
Person(String name, int age) { public class Main {
this.name = name; public static void main(String[] args) {
this.age = age; // Create a Person object
} Person person1 = new Person("Alice", 30);

// Method to display person's details // Display initial details


void displayInfo() { person1.displayInfo();
System.out.println("Name: " + name);
System.out.println("Age: " + age); // Update the age
} person1.setAge(31);
// Method to set a new age
void setAge(int age) { // Display updated details
this.age = age; System.out.println("Updated Age: " +
person1.getAge());
}
}
}

You might also like