DEV Community

Cover image for The Blueprints of Object Oriented Programming
Zaynul Abedin Miah
Zaynul Abedin Miah

Posted on

The Blueprints of Object Oriented Programming

Classes

A blueprint or class is a visual representation of what real-world things or ideas look like. It can be used to represent things like houses, bank accounts, employees, clients, cars, and products. The blueprints are used to create different objects in a program. These classes can represent different things, such as machines or data structures. To complete the process, you need to do three things: find the important things, study what they can do and how they are, and then create a class in the code. After the class is defined, it can be used again and again to create multiple objects. It works like a factory, producing as many objects as needed based on the same blueprint. To illustrate, you can create a class or blueprint that defines the contents of a backpack. This blueprint can then be used to create multiple backpack objects.

`


class Backpack:
pass

`
Object-oriented programming is powerful because it allows you to create something once and then use it multiple times. This is possible due to its reusability. As a developer, it's your job to analyze the object and determine which attributes and behaviors are important to include in your system.

If you want to practice, think about the objects around you and how they can be represented as classes. Please provide a list of the attributes and behaviors that are relevant to a system.

Main Elements of Classes:

  • Class Attributes
  • init()
  • Methods

Guidelines regarding Classes

Class names are a type of word that represents a person, place, thing, or idea. They always start with a capital letter and have uppercase letters for each new word that follows. This is called PascalCase naming convention. Some examples are House, Human, Dog, and Account.
The body of the class must be indented.

In Python, we use Pascal Case (also called Upper Camel Case) as the naming convention for class names.

Pascal Case is a naming convention in which the first letter of each word is capitalized.

For example:

  • House
  • BankAccount
  • SchoolBackpack
  • LinkedList

💡 Tip: The first letter of the class name should be capitalized.

To learn more about this naming convention, please refer to this article: PascalCase.

When creating a program, it's important to analyze the characteristics of objects, whether they are physical or abstract, in order to identify the relevant classes. Every object has its own special characteristics and can do certain things. The problem statement is created by talking with the client or understanding the problem. It helps identify important details from the description.

Examples of identifying classes from specific problems
To make it easier to understand, let's use the example of a fast food shop system. In this situation, we can have different objects or classes such as Food, Pizza, Burgers, Hot Dogs, Soda, Water, French Fries, Clients, and Employees. The importance of these classes varies depending on the project's scope.

After looking at it more closely, we might realize that the category "Food" is too broad and not very useful for representing specific items. Therefore, we can remove it.

Now, let's look at another example of a store that sells electronics. They have a wide range of devices and accessories available. The possible categories for this system are: Laptop, Desktop, Tablet, Smartphone, Notebook, Pen Drive, Headphone, Accessories, Employees, Manager, and Clients.

Lastly, let's imagine a video game situation where a player moves through different levels while trying to avoid enemies. For this situation, the important classes might include Game, Levels, Player, Goal, Enemies, Ants, Toads, and Lives. Upon further analysis, we may determine that the class "Lives" is not needed and can instead be represented as an attribute of the player class.

To identify classes, you need to analyze the problem statement and extract the important elements for the program or system. During this process, it may be necessary to discard or modify some initial classes.

It's important to note that writing a class in Python involves a header and a body, similar to functions. The header specifies the name of the class and indicates if the class inherits from another class, which will be covered in a later section.

To write the header, you start with the "class" keyword followed by the name of the class. It is recommended to capitalize the first letter of the class name and use uppercase for each word in the name. A colon (:) is essential at the end of the header to avoid syntax errors.

For example, let's consider a class named "Backpack." The class header would be written as follows:



class Backpack:


Enter fullscreen mode Exit fullscreen mode

Once you have written the class header, you will need to define the class body. The class body is where you define the attributes and behaviors of the objects created from the class. It acts as a blueprint for the class. Attributes are the qualities or characteristics of objects, while methods are the actions that objects can do.

The basic structure of a class is as follows:



class ClassName:
    # Class attributes
    # Class methods


Enter fullscreen mode Exit fullscreen mode

Note that all elements in the class body need to be indented, just like the body of a function. In the example above, the attributes and methods are represented as comments, but in your actual code, they will be implemented accordingly.

Throughout the course, you will dive deeper into each of these elements and learn how to implement them effectively in your classes.

Use of "object" in Python 3 and Python 2
🔹 Python 2
In Python 2, there are two types of classes and if you want to use the new-style class, you need to add the word object between parentheses after the name of the class:

class (object):

For example:

class Dog(object):

🔸 Python 3
In Python 3, you can include it directly if you want your code to work with Python 2. However, in the newer version of Python, leaving it out has the same effect.

class :

For example:

class Dog:

For more information on this topic, please refer to this https://stackoverflow.com/questions/45833595/best-practice-for-python-3-class-creation/45834074#45834074

To write the class headers for the classes discussed for the pet shelter system. Here are the class headers for each class:

Image description

Animal (base class for all animals in the shelter):



class Animal:
    pass


Enter fullscreen mode Exit fullscreen mode

Dog (subclass of Animal):



class Dog(Animal):
    pass


Enter fullscreen mode Exit fullscreen mode

Cat (subclass of Animal):



class Cat(Animal):
    pass


Enter fullscreen mode Exit fullscreen mode

Parrot (subclass of Animal):



class Parrot(Animal):
    pass


Enter fullscreen mode Exit fullscreen mode

Lizard (subclass of Animal):



class Lizard(Animal):
    pass


Enter fullscreen mode Exit fullscreen mode

Snake (subclass of Animal):



class Snake(Animal):
    pass


Enter fullscreen mode Exit fullscreen mode

Employee:



class Employee:
    pass


Enter fullscreen mode Exit fullscreen mode

Volunteer:



class Volunteer:
    pass


Enter fullscreen mode Exit fullscreen mode

Donor:



class Donor:
    pass


Enter fullscreen mode Exit fullscreen mode

The class headers help us begin defining each class by specifying their attributes, methods, and behaviors in the following execution.

Top comments (0)