Object Oriented Programming/Classes
In Object-Oriented Programming (OOP), a class is a sort of blueprint for creating objects. It defines a new data type that encapsulates data and behavior (methods) related to a particular entity or concept. Classes can serve as templates, allowing us to create multiple objects with similar characteristics and behaviors. Classes are essentially a giant container for a bunch of functions that can either rely on each other or independently operate within the class.
Components of a Class
[edit | edit source]A class consists of two primary components: attributes (also known as properties or data members) and methods (also known as member functions).
1. Attributes: Attributes are variables that represent the state of an object. They store information about the object's characteristics. In the animal analogy, attributes could include the species
, name
, age
, weight
, color
, etc. Attributes define what data an object can hold.
2. Methods: Methods are functions that define the behavior of the object. They represent the actions an object can perform. In the animal analogy, methods could include "make_sound()
", "move_type()
", "eat_what()
", etc. Methods define what operations an object can perform.
Most classes are made on their own python files and to be used or accessed by other python files. Being a class on its own page allows other python files to call and reference the class without directly affecting the code in the class. Classes can be used for calculating equations, registering user input and acting with it, and raising exceptions to other python files with, again, not being directly affected by other python files' codes.
Subclasses are essentially a child of a Class. Think of Classes as Parents, and Subclasses as Children. Parent Classes can pass properties or functions onto their Child classes. Or think of it as a Child inherits things from their Parent. Subclasses are for more complex programs that utilize multiple ideas under one grand idea.
Below is an template of creating a class in Python (or most other OOP languages), we use the class
keyword followed by the class name and a colon. Within the class, we define attributes and methods.
class MyClass:
# Constructor method (__init__) initializes object attributes
def __init__(self, attribute1, attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2
# Method 1
def method1(self):
# Method code here
# Method 2
def method2(self):
# Method code here
Here is an real example in Python of creating Animal class blueprint. This is a blueprint for all animals , called the "Animal" class, and it will contain common attributes and behaviors shared by all animals in the zoo.
In this class, we've defined attributes such as "species
," "name
," and "age
." We also have two methods: "make_sound()
" , "move_type()
",eat_what()
. The methods are empty for now (marked with pass
) as each animal will implement them differently.
class Animal:
def __init__(self, species, name, age):
self.species = species
self.name = name
self.age = age
def make_sound(self):
pass
def move(self):
pass
Now, we shall make a individual class Zebra
as followed:
class Zebra(Animal):
def __init__(self, name, age):
super().__init__("Zebra", name, age)
def make_sound(self):
return "Bray"
def move(self):
return "Galloping."
As you can see In those classes, we use inheritance to inherit the attributes and methods from the Animal class. We then override the "make_sound()" and "move()" methods to implement the specific sound and movement behavior for each animal.
Now , we already create a Zebra class animals and we can now create individual animal objects and interact with them in the main program.
def main():
# Create animal objects
zebra1 = Zebra("Marty", 10)
# Interact with other animals
print({zebra1.name} " the " {zebra1.species} " says: " {zebra1.make_sound()})
#Marty the Zebra says : bray
#Interact with enviroments
print({zebra1.name} " the " {zebra1.species} " aged " {zebra1.age} {zebra1.move()})
#Marty the Zebra aged 10 galloping
if __name__ == "__main__":
main()