Topics

Python-Introduction to OOP

Welcome to another tutorial, here you learn the basics of object-oriented programming. Right from the beginning of programming, computer scientists have tried to develop several ways to program a computer. Over the years, various developed programming languages came into existence and have eventually changed how people write programs. Recently, object-oriented programming is a popular methodology for various application programming.

In other for a particular methodology in programming to be followed, there has to be a need to design a particular programming language in a specific way. This brings us to Python, which is an Objective Oriented Programming Language as a matter of fact.

 

What is Object Oriented Programming?

Object Oriented programming or in short form, OOP, is typically a programming style that is associated with the concept of Object, Class, and other concepts that revolve around these two, such as inheritance, Abstraction, Encapsulation, Polymorphism, and so on.

The programming language was designed in a way that one should focus on an object during programming and not the procedure. The object, in this case, is anything we see around, ranging from a human (possessing properties like name, address, DOB, etc), a chair (portrayed by size, material, cost, and so on.), and so on.

OOP distinctively brings programming close to reality, because we are dealing with objects, performing operations on them, using its method and variables, and many more. 

 

Concepts of OOP

 

Suppose, human beings are living forms, and are broadly categorized into two types, Male and Female. This is what we already know. Now, every human being has two hands, two legs, two eyes, one heart, one nose, and so on. Also, we know that some body parts are common in men and women, and also, there are specific body parts that are present in men and are not in women, and vice versa.

Furthermore, every human being eats, sees, walks, and so on. Also, both of them perform some common functions, however, there are some that are specific to each other. E.g. Females can give birth, but the Male cannot, an attribute is just for the female counterpart.

Now, know how this is related to OOPS and Python. In the next section, we will discuss all possible OOPS concepts through examples.

 

Class

In this case, a human being can be taken as a class. This is because a class is more or less a blueprint for any functional entity, which defines the entity's properties and functions. E.g. human beings having body parts and performing different tasks.

Inheritance

Now let’s consider the class HumanBeing, which consists of properties such as the legs, hands, nose, etc; and functions such as talking, walking, seeing, hear. and so on. Also, both the Male and Female are classes, however, their properties and functions are included in the HumanBeing. As such, they can inherit whatever traits from the HumanBeing class by making use of the Inheritance.

 

Objects

The example; My name is Abel, and I am an instance/object of class Male. For instance when the phrase human being, or Male or Female, we are referring to a kind: you, your friend, we, me, are forms of these classes. 

In addition, we have a physical presence, but a class is merely a logical definition. So, we are the objects.

 

Abstraction

In Python, Abstraction refers to displaying only the required things to the outside world while hiding the detail. Referring back to the example of Human beings, human beings can hear, talk eat, and walk, however, the detail is hidden from the outside world. That is, in this case, we can take the skin as an abstraction factor, and thereby hide the inside mechanism.

 

Encapsulation

This concept is quite confusing to example with our example. but, you know that our legs are bound to help us walk; as our hands help us to hold things. Therefore, this binding of properties to functions is called encapsulation.

 

Polymorphism

This is a concept, that allows redefining the way something works. This is either by the change in approach.