Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

_Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a paradigm that organizes code using 'objects' which encapsulate data and methods. Key concepts include classes and objects, encapsulation, inheritance, and polymorphism, which promote modularity, abstraction, reusability, and maintainability. Different programming languages implement OOP features uniquely, including class declaration, object instantiation, access modifiers, and abstract classes.

Uploaded by

examnairobiccmrs
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

_Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a paradigm that organizes code using 'objects' which encapsulate data and methods. Key concepts include classes and objects, encapsulation, inheritance, and polymorphism, which promote modularity, abstraction, reusability, and maintainability. Different programming languages implement OOP features uniquely, including class declaration, object instantiation, access modifiers, and abstract classes.

Uploaded by

examnairobiccmrs
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/ 4

Introduction to Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of


"objects," which can contain data in the form of fields (often called attributes or properties) and
code in the form of methods (functions associated with an object). OOP focuses on organizing
code in a way that models real-world entities and their interactions.

The main goals of OOP are:

1.​ Modularity: Breaking down a program into manageable and reusable parts.
2.​ Abstraction: Hiding complex implementation details and exposing only the necessary
features.
3.​ Reusability: Using existing code across different programs to reduce redundancy.
4.​ Scalability and Maintenance: Making code easier to modify and extend over time.

Object-Oriented Programming Concepts

OOP is built around four core concepts:

1. Classes and Objects

●​ Class: A blueprint for creating objects. It defines the properties and behaviors that its
objects will have. For example, a Car class might have attributes like color and model
and methods like start() and stop().
●​ Object: An instance of a class. For example, a specific car like a red Toyota Corolla is an
object of the Car class.

class Car:
def __init__(self, color, model):
self.color = color
self.model = model

def start(self):
print(f"The {self.color} {self.model} is starting.")

# Creating an object
my_car = Car("Red", "Toyota Corolla")
my_car.start()

2. Encapsulation

●​ Encapsulation involves bundling the data (attributes) and methods (functions) that
operate on the data into a single unit (class) and restricting direct access to some
components. This is achieved using access modifiers:
○​ Public: Accessible from anywhere.
○​ Private: Accessible only within the class (denoted by __ prefix in Python).
○​ Protected: Accessible within the class and subclasses (denoted by _ prefix in
Python).

class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private attribute

def deposit(self, amount):


if amount > 0:
self.__balance += amount

def get_balance(self):
return self.__balance

account = BankAccount(1000)
account.deposit(500)
print(account.get_balance())

3. Inheritance

●​ Inheritance allows a class (child class) to acquire the properties and behaviors of
another class (parent class). This promotes code reuse.
●​ The child class can add new properties or override existing ones.

class Vehicle:
def __init__(self, brand):
self.brand = brand

def move(self):
print("The vehicle is moving.")

class Car(Vehicle):
def move(self):
print(f"The {self.brand} car is driving.")

my_car = Car("Toyota")
my_car.move()

4. Polymorphism

●​ Polymorphism means "many forms." It allows methods to have the same name but
behave differently based on the object calling them.
●​ This is achieved through method overriding (redefining a method in a subclass) or
method overloading (defining multiple methods with the same name but different
parameters).

class Animal:
def speak(self):
print("The animal makes a sound.")

class Dog(Animal):
def speak(self):
print("The dog barks.")

animal = Animal()
dog = Dog()

animal.speak()
dog.speak()

Language Structures of Object-Oriented Programming

Different programming languages implement OOP features in unique ways. Below are the
general structures you’ll encounter in OOP languages:

1. Class Declaration

Defines the structure and behavior of objects. Most OOP languages use a class keyword.

class ClassName:
def __init__(self):
pass

2. Object Instantiation

Creates an instance of a class using a constructor (often __init__ in Python or similar


constructs in other languages).

object_name = ClassName()

3. Attributes and Methods

Attributes are variables defined within a class, while methods are functions associated with an
object.

class Example:
def __init__(self, value):
self.value = value
def display(self):
print(self.value)

obj = Example(10)
obj.display()

4. Access Modifiers

Control visibility of class members:

●​ Public: Accessible everywhere.


●​ Private: Accessible only within the class.
●​ Protected: Accessible in the class and its subclasses.

5. Inheritance Syntax

Supports reusing code by extending classes. Most languages use a colon (:) or keyword like
extends.

class Parent:
pass

class Child(Parent):
pass

6. Abstract Classes and Interfaces

●​ Abstract classes are base classes that cannot be instantiated and may include abstract
methods (methods without implementation).
●​ Interfaces define a contract for classes to implement.

from abc import ABC, abstractmethod

class AbstractClass(ABC):
@abstractmethod
def abstract_method(self):
pass

class ConcreteClass(AbstractClass):
def abstract_method(self):
print("Implementation")

You might also like