Object-Oriented Programming is a programming paradigm that uses objects to represent real-world entities.
- Classes and Objects:
- Classes define blueprints for creating objects.
- Objects are instances of classes.
- Attributes and Methods:
- Classes can have attributes (data) and methods (functions) that define their properties and behavior.
Example of creating a simple class and an object:
# Define a simple class
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print(f"{self.name} says woof!")
# Create an object (instance) of the Dog class
my_dog = Dog("Buddy")
my_dog.bark() # Call the bark method on the object
Classes can have attributes and methods that define their behavior.
- Class Attributes:
- Class attributes are shared among all instances of the class.
- Instance Attributes:
- Instance attributes are specific to individual objects.
- Methods:
- Methods are functions defined within a class.
Example of class attributes and methods:
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14159 * self.radius**2
def circumference(self):
return 2 * 3.14159 * self.radius
my_circle = Circle(5)
print(f"Area: {my_circle.area()}")
print(f"Circumference: {my_circle.circumference()}")
Inheritance allows you to create new classes that inherit attributes and methods from existing classes.
- Parent Class (Superclass):
- The parent class defines common attributes and methods.
- Child Class (Subclass):
- The child class inherits from the parent class and can have additional attributes and methods.
Example of inheritance:
# Parent class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass
# Child class inheriting from Animal
class Dog(Animal):
def speak(self):
return f"{self.name} says woof!"
my_dog = Dog("Buddy")
print(my_dog.speak()) # Calls the speak method of the Dog class
Polymorphism allows objects of different classes to be treated as objects of a common superclass.
- Common Superclass:
- Create a common superclass that defines shared methods or attributes.
- Subclasses with Different Implementations:
- Subclasses provide their own implementations of methods.
Example of polymorphism:
# Common superclass
class Shape:
def area(self):
pass
# Subclasses with different implementations of area
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14159 * self.radius**2
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
shapes = [Circle(5), Rectangle(4, 6)]
for shape in shapes:
print(f"Area: {shape.area()}")
Object-Oriented Programming (OOP) is a fundamental concept in Python and many other programming languages. It allows you to model real-world entities, promote code organization, and enhance code reusability. Practice with these examples to become proficient in using OOP principles in Python.