Object-Oriented Programming System (OOPs) is one of the most important concepts that everyone in the world who write a computer program must have a basic understanding of it.

Let’s first define what is OOPs?

Object-oriented programing is a programming paradigm based on the concept of objects, which contains data and code.

A new question that will obviously strike our mind that what is objects.

An object is an entity that has a state and behaviour associated with it.

An object can be a real-world entity, integer, array, string etc.

An important concept in oops is class, so let’s understand what is class.

A class is a blueprint that contains methods and variables that are common to all kind.

An example to understand it better,

Let Car be a class then Audi and Maruti are the objects of the class Car. All cars have common features such as a steering wheel, four wheels etc.

A simple class code:

## A simple example of class
class Test:
    ## A sample method
    def func(self):
        print('Hello world')

obj = Test()
obj.func()

A couple of key questions,

What is the difference between the methods and functions?

A method is similar to the function which executes the given instructions to perform a particular task. The only difference between the method and object is that method is associated with objects while functions are not.

What is self?

The self parameter is the reference to the current instance of the class. This is similar to pointers and this in C++ and Java respectively. There must be a self parameter associated with the method of the class for which we don’t give the value, python provides it.

There are three types of access modifiers which we generally use,

  1. Public: This can be accessed from anywhere in the program.
  2. Protected(‘_var-name): This can be accessed in the class and its subclasses.
  3. Private(‘__var-name): This can only be accessed within the class.

Also, there are three types of methods that can be defined in the class,

  1. Instance Method: From its name, it can access unique data from its instance. This method must have an extra parameter self.
  2. Class Method: We often create this method when need to modify only the class variables. They can’t access specific instance data, but they can call other static methods.
  3. Static Method: We can simply refer this method to as a function. This can not be used to access any class-specific data nor we have to use parameter self nor we have to instantiate an instance for this method.

#python #programming #class #object-oriented #objects #oops python

OOPs Python Tutorial
1.35 GEEK