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
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,  ,  ,  ,  , 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 " ", " ", " ", 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  keyword followed by the class name and a colon. Within the class, we define attributes and methods. 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 " ," " ," and " ." We also have two methods: " ", " ",. The methods are empty for now (marked with ) as each animal will implement them differently.

Now, we shall make a individual class  as followed:

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.

YouTube Tutorials

 * Python OOP Tutorial 1: Classes and Instances