Object - Oriented Programming
Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around data, or objects, rather than functions and logic. An object is an instance of a class, which is a blueprint that defines its properties (attributes) and behaviors (methods). OOP focuses on four main principles:
![]() |
Object - Oriented Programming |
- Encapsulation - Bundling data and methods that operate on that data within a single unit, or class.
- Abstraction - Hiding complex implementation details and showing only the essential features of an object.
- Inheritance - Allowing a new class to inherit properties and behaviors from an existing class.
- Polymorphism - Allowing objects to be treated as instances of their parent class, enabling a single function to work in different ways based on the object it is acting upon.
This approach helps in creating modular, reusable, and maintainable code.
1) History :
The history of Object-Oriented Programming (OOP) dates back to the 1960s and has evolved through several key developments:
1. Early Concepts (1960s): The foundation of OOP was laid by Ole-Johan Dahl and Kristen Nygaard at the Norwegian Computing Center in the 1960s. They developed the programming language Simula, specifically Simula 67, which introduced the concept of classes and objects. Simula was initially created for simulating complex systems, and it introduced features like classes, objects, inheritance, and dynamic binding.
2. Development of Smalltalk (1970s): In the 1970s, Alan Kay and his team at Xerox PARC developed Smalltalk, which played a crucial role in popularizing OOP. Smalltalk was designed to be a fully object-oriented language where everything was an object, and it introduced key concepts such as message passing, dynamic typing, and a uniform object model. It also inspired the development of graphical user interfaces (GUIs).
3. Adoption and Evolution (1980s-1990s): During the 1980s, OOP gained more traction as languages like C++ were developed by Bjarne Stroustrup. C++ was an extension of the C language that added OOP features like classes, inheritance, and polymorphism, making it more accessible to the programming community. Other languages like Objective-C and Eiffel also emerged during this period, incorporating OOP concepts.
4. Mainstream Adoption (1990s-Present): The 1990s saw the mainstream adoption of OOP with the release of Java by Sun Microsystems. Java emphasized portability and platform independence with its "write once, run anywhere" philosophy, making it extremely popular. Other languages like Python, Ruby, and C# also embraced OOP, further solidifying its place in software development.
5. Modern OOP (2000s-Present): Today, OOP is a fundamental paradigm in software development, widely used in various domains from web development to system software. Modern languages have built upon OOP principles, integrating them with other paradigms like functional programming to create versatile, powerful languages.
OOP's evolution has led to more organized, reusable, and maintainable code, making it a core aspect of modern software engineering.
2) OOPS Concepts :
Object-Oriented Programming (OOP) revolves around four key concepts that help in organizing and structuring software design:
1. Encapsulation
Definition: Encapsulation is the bundling of 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, which can prevent the accidental modification of data.
Benefit: Enhances data security and integrity by providing a controlled interface for interacting with the data.
2. Abstraction
Definition: Abstraction involves hiding the complex implementation details of a system and exposing only the essential features. In OOP, this is achieved using abstract classes and interfaces.
Benefit: Simplifies code management by reducing complexity, allowing programmers to focus on high-level functionality without worrying about the intricate details.
3. Inheritance
Definition: Inheritance is a mechanism that allows one class (child or subclass) to inherit the properties and behaviors of another class (parent or superclass). This promotes code reusability and establishes a relationship between classes.
Benefit: Facilitates code reusability and the creation of hierarchical class structures, reducing redundancy and improving maintainability.
4. Polymorphism
Definition: Polymorphism allows objects to be treated as instances of their parent class. The same method or operator can behave differently based on the object it is acting upon. It can be achieved through method overriding (runtime polymorphism) and method overloading (compile-time polymorphism).
Benefit: Enhances flexibility and integration, allowing for a more generic and scalable codebase that can handle different data types and scenarios.
Together, these concepts make OOP a powerful paradigm for building modular, reusable, and maintainable software.
3) Object oriented programming python
4) Object oriented programming examples
Here are some examples of Object-Oriented Programming (OOP) in Python to illustrate key concepts like classes, objects, encapsulation, inheritance, and polymorphism:
1. Basic Class and Object Example
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
def get_details(self):
return f"'{self.title}' by {self.author}"
# Creating an object of the Book class
book1 = Book("1984", "George Orwell")
print(book1.get_details()) # Output: '1984' by George Orwell
2. Encapsulation Example
- Using private attributes to restrict direct access and providing public methods to interact with the data.
class Account:
def __init__(self, owner, balance):
self.owner = owner
self.__balance = balance # Private attribute
def deposit(self, amount):
if amount > 0:
self.__balance += amount
print(f"Deposited {amount}")
def withdraw(self, amount):
if 0 < amount <= self.__balance:
self.__balance -= amount
print(f"Withdrew {amount}")
else:
print("Insufficient balance")
def get_balance(self):
return self.__balance
account = Account("Alice", 1000)
account.deposit(500)
account.withdraw(300)
print(account.get_balance())
Output:
1200
Accessing the private attribute directly will raise an AttributeError
print(account.__balance)
3. Inheritance Example
- Creating a subclass that inherits from a superclass.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "Animal sound"
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
dog = Dog("Buddy")
cat = Cat("Whiskers")
print(dog.speak()) # Output: Woof!
print(cat.speak()) # Output: Meow!
4. Polymorphism Example
- Using method overriding to provide different implementations of a method in subclasses.
class Shape:
def area(self):
pass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
shapes = [Rectangle(4, 5), Circle(3)]
for shape in shapes:
print(shape.area())
Output:
20
28.26
5. Abstraction Example
- Using abstract classes to define methods that must be implemented in subclasses.
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start_engine(self):
pass
class Car(Vehicle):
def start_engine(self):
print("Car engine started")
class Motorcycle(Vehicle):
def start_engine(self):
print("Motorcycle engine started")
car = Car()
car.start_engine() # Output: Car engine started
motorcycle = Motorcycle()
motorcycle.start_engine() # Output: Motorcycle engine started
These examples demonstrate how OOP can be used to model real-world scenarios, encapsulate data, establish relationships between classes, and create flexible and reusable code structures.
For More References(Wikipedia)
0 Comments