0% found this document useful (0 votes)
16 views13 pages

What Is OOP?: Example

Object-Oriented Programming (OOP) is a programming paradigm centered around objects, which are instances of classes that encapsulate data and behavior. Key concepts include encapsulation, inheritance, polymorphism, and abstraction, which enhance code reusability, maintainability, and real-world modeling. While OOP has advantages such as better code management and collaboration, it also presents challenges like a steep learning curve and potential resource overhead.

Uploaded by

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

What Is OOP?: Example

Object-Oriented Programming (OOP) is a programming paradigm centered around objects, which are instances of classes that encapsulate data and behavior. Key concepts include encapsulation, inheritance, polymorphism, and abstraction, which enhance code reusability, maintainability, and real-world modeling. While OOP has advantages such as better code management and collaboration, it also presents challenges like a steep learning curve and potential resource overhead.

Uploaded by

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

5. What is OOP?

Object-Oriented Programming (OOP) is a programming paradigm that


uses the concept of "objects" to design and develop applications.
Objects are instances of classes, which are templates that define
their properties (attributes) and behaviors (methods).
Key Concepts of OOP:
 Class: A blueprint for creating objects.
Example: A "Car" class might have attributes like color, brand,
and methods like start() or stop().
 Object: An instance of a class.
Example: Car myCar = new Car();
 Encapsulation: Bundling data (attributes) and methods into a
single unit (class) and restricting access to them.
Example: Using private fields and public methods to access
data.
 Inheritance: A mechanism where a new class derives properties
and behavior from an existing class.
Example: A "SportsCar" class inheriting from the "Car" class.
 Polymorphism: The ability to use the same method or operator
in different ways.
Example: A draw() method that behaves differently for "Circle"
and "Square" objects.
 Abstraction: Hiding the complex implementation details and
exposing only essential features.
Example: A sendEmail() method that works without knowing
the inner workings.
2. Evolution of OOP
 Procedural Programming: Early programming paradigms (e.g.,
C) were procedure-based, focusing on functions and the flow of
tasks.
 Structured Programming: Emphasized breaking down code into
smaller, reusable functions but lacked the ability to model real-
world entities.
 Advent of OOP: Emerged in the 1960s and 1970s to address the
need for better modeling of real-world systems.
o Notable milestones:
 1960s: Simula, the first OOP language, introduced
the concepts of objects and classes.
 1980s: C++ combined OOP with procedural
programming.
 1990s: Java, a fully object-oriented language, gained
popularity for its portability and simplicity.

3. Merits of OOP
1. Reusability: Code can be reused across projects by leveraging
classes and inheritance.
Example: A "Vehicle" class can be reused in applications
involving cars, bikes, or planes.
2. Modularity: Programs are divided into smaller, manageable
parts (classes).
3. Scalability: Easy to add new features or modify existing code
without affecting other parts.
4. Maintainability: Encapsulation ensures better code
organization and reduces complexity.
5. Real-World Modeling: Closely mirrors real-world entities and
their interactions.
6. Abstraction: Simplifies complex problems by focusing on high-
level design.
7. Data Security: Encapsulation restricts unauthorized access to
sensitive data.

4. Demerits of OOP
1. Learning Curve: Can be challenging for beginners to grasp
concepts like inheritance and polymorphism.
2. Overhead: OOP programs may be slower due to abstraction
layers and dynamic method resolution.
3. Complexity: Overuse of objects and inheritance can lead to
code bloat and confusion.
4. Resource Intensive: Object creation and management can
consume more memory compared to procedural programming.

5. Reasons for Embracing OOP


1. Better Code Management: Encourages organized and modular
code, making large projects more manageable.
2. Improved Collaboration: Multiple developers can work on
different classes without conflicts.
3. Ease of Maintenance: Bugs are easier to identify and fix due to
encapsulated code.
4. Real-World Applications: Suitable for software systems like
simulations, games, and enterprise applications.
5. Standardization: Widely adopted by modern software
development, making it industry-standard.

6. Examples of OOP Languages


 Java: Known for its "write once, run anywhere" principle.
Example: Enterprise applications, Android development.
 C++: Combines OOP and procedural programming for high-
performance applications.
Example: Game engines, system software.
 Python: Easy-to-learn OOP concepts, versatile for web
development, data science, etc.
Example: Django web framework, machine learning models.
 Ruby: Pure OOP language, known for developer-friendly syntax.
Example: Ruby on Rails web development.
 C#: Designed for the .NET framework, widely used for Windows
applications and games.
Example: Unity game development.
 JavaScript (with OOP features): Often used for web
applications with frameworks like React and Node.js.
Example: Dynamic web applications.

Summary
Object-Oriented Programming is a powerful paradigm for modern
software development, offering benefits like reusability, scalability,
and real-world modeling. While it has a steeper learning curve and
resource overhead, its advantages make it a preferred choice for
many applications. Examples of OOP languages include Java, Python,
and C++, each suited for various domains.
Concepts of OOP
1. Reusability
Reusability means using existing code (like classes and methods) in
new programs without rewriting it. It saves time and effort.
Example:
Suppose you create a class Vehicle with attributes like speed and fuel.
 You can reuse this class for different vehicles like Car or Bike
without creating everything from scratch.
java
Copy code
class Vehicle {
int speed;
int fuel;

void displayInfo() {
System.out.println("Speed: " + speed + " Fuel: " + fuel);
}
}

class Car extends Vehicle {


String brand;
}

2. Encapsulation
Encapsulation means wrapping data (attributes) and methods
(behaviors) together in a single unit (class) and restricting direct
access to some parts of the data.
 How it works: Use private for attributes and provide public
methods (getters and setters) to access them.
 Why? To protect sensitive information and maintain control.
Example:
java
Copy code
class BankAccount {
private double balance; // Cannot be accessed directly

public void deposit(double amount) {


balance += amount;
}

public double getBalance() {


return balance;
}
}
3. Instantiating
Instantiating is the process of creating an object from a class. The
object is a specific instance of the class with its own values.
Example:
java
Copy code
class Dog {
String name;
}

public class Main {


public static void main(String[] args) {
Dog myDog = new Dog(); // Instantiating a Dog object
myDog.name = "Buddy";
System.out.println("Dog's name: " + myDog.name);
}
}

4. Abstraction
Abstraction hides unnecessary details and shows only the essential
features. This helps simplify complex systems.
 How? Use abstract classes or interfaces.
 Why? To focus on "what" a class does rather than "how" it does
it.
Example:
java
Copy code
abstract class Animal {
abstract void sound(); // Abstract method, no implementation
}

class Cat extends Animal {


void sound() {
System.out.println("Meow");
}
}

5. Inheritance
Inheritance allows a class (child) to inherit properties and methods
from another class (parent). This enables code reusability and the
creation of specialized classes.
Example:
java
Copy code
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}

class Dog extends Animal {


void bark() {
System.out.println("The dog barks.");
}
}
When you create a Dog object, it can use both eat() and bark().

6. Composition
Composition is when a class is made up of objects of other classes.
It’s a "has-a" relationship.
Example:
A Car is composed of an Engine and Wheels.
java
Copy code
class Engine {
void start() {
System.out.println("Engine starts");
}
}
class Car {
Engine engine = new Engine(); // Composition

void drive() {
engine.start();
System.out.println("Car is moving");
}
}

7. Aggregation
Aggregation is a specialized form of composition where one class
uses another but doesn’t own it. It’s a "has-a" relationship but
weaker than composition.
Example:
A Team has Players, but players can belong to multiple teams.
java
Copy code
class Player {
String name;
Player(String name) {
this.name = name;
}
}
class Team {
String teamName;
Player[] players;

Team(String teamName, Player[] players) {


this.teamName = teamName;
this.players = players;
}
}

8. Overriding
Overriding allows a subclass to provide its own implementation of a
method from the parent class.
 Why? To change or enhance the behavior of a parent class
method.
Example:
java
class Animal {
void sound() {
System.out.println("Some sound...");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark!");
}
}

9. Polymorphism
Polymorphism allows objects to take many forms. A method can
behave differently depending on the object calling it.
 Types: Method Overloading and Method Overriding.
 Why? To handle different cases with a single interface.
Example:
java
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}

class Circle extends Shape {


void draw() {
System.out.println("Drawing a circle");
}
}

class Square extends Shape {


void draw() {
System.out.println("Drawing a square");
}
}
You can call draw() on different shapes, and the appropriate behavior
is executed.

You might also like