Chapter 7: Object-Oriented Programming (OOP)
7.1 Introduction to
Object-Oriented Programming 7.1.1 Objects, classes, and attributes Explanation:
Object-oriented programming focuses on creating objects that encapsulate data
and behavior. Objects are instances of classes, which define their attributes (data)
and methods (behavior). Example:
python
///Example
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello, my name is",
self.name)
person1 =
Person("Alice", 25)
person1.greet()
7.1.2 Encapsulation,
inheritance, and polymorphism Explanation: OOP concepts include encapsulation
(data hiding), inheritance (creating new classes from existing ones), and
polymorphism (objects of different classes can be treated interchangeably).
Example:
python
///Example
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 ** 2
shapes = [Rectangle(4,
5), Circle(3)]
for shape in shapes:
print("Area:", shape.area())
7.2 Classes and Objects
7.2.1 Defining classes and creating objects Explanation: Classes are defined
using the class keyword and can have attributes and methods. Objects are
created by instantiating a class using the class name followed by parentheses.
Example:
python
///Example
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def drive(self):
print("Driving", self.brand,
self.model)
car1 =
Car("Toyota", "Corolla")
car1.drive()
7.2.2 Class attributes
and instance attributes Explanation: Class attributes are shared by all
instances of a class, while instance attributes are specific to each instance.
Class attributes are defined outside of any methods. Example:
python
///Example
class Dog:
species = "Canine" # Class attribute
def __init__(self, name):
self.name = name # Instance attribute
dog1 =
Dog("Max")
print(dog1.name) # Instance attribute
print(dog1.species) # Class attribute
7.2.3 Class methods and
static methods Explanation: Class methods are defined using the @classmethod
decorator and can access class-level data. Static methods are defined using the
@staticmethod decorator and don't have access to instance or class data.
Example:
python
///Example
class MathUtils:
@classmethod
def add(cls, a, b):
return a + b
@staticmethod
def multiply(a, b):
return a * b
result1 =
MathUtils.add(2, 3) # Class method
result2 =
MathUtils.multiply(4, 5) # Static method
print(result1)
print(result2)
7.3 Inheritance and
Polymorphism 7.3.1 Inheriting from a superclass Explanation: Inheritance allows
you to create new classes (subclasses) based on existing ones (superclasses).
Subclasses inherit attributes and methods from their superclasses. Example:
python
///Example
class Animal:
def make_sound(self):
pass
class Dog(Animal):
def make_sound(self):
print("Woof!")
dog1 = Dog()
dog1.make_sound()
7.3.2 Overriding
methods in subclasses Explanation: Subclasses can override methods inherited
from superclasses by providing a new implementation. The overridden method in
the subclass is used instead of the superclass method. Example:
python
///Example
class Animal:
def make_sound(self):
print("Generic animal sound")
class Dog(Animal):
def make_sound(self):
print("Woof!")
animal1 = Animal()
animal1.make_sound() # Generic animal sound
dog1 = Dog()
dog1.make_sound() # Woof!
7.3.3 Polymorphism and
method overriding Explanation: Polymorphism allows objects of different classes
to be treated interchangeably. Method overriding enables subclasses to provide
their own implementation of inherited methods. Example:
python
///Example
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 ** 2
shapes = [Rectangle(4,
5), Circle(3)]
for shape in shapes:
print("Area:", shape.area())
This chapter covers the
fundamental concepts of object-oriented programming (OOP). OOP allows you to
create classes and objects, define attributes and methods, and leverage
inheritance and polymorphism for code reuse and flexibility. The examples
provided demonstrate the usage and syntax of each topic, helping readers
understand how to apply these concepts in their own programs.
0 comments:
Post a Comment