Object Oriented Programming in Python (OOPs)

 Object Oriented Programming(OOP)

Object-Oriented Programming (OOP) in Python is a programming paradigm that emphasizes the use of objects and classes to model real-world phenomena and organize code into reusable and modular structures.

Classes and objects are used to implement OOP in Python. A class is a blueprint for constructing objects, whereas an object is a class instance. Classes define object properties (attributes) and behavior (methods).


Here is an example of a simple Python class:

example1.png

In the above example, we define a world-class that has two attributes (name and age) and one method (say_hello). We then create an object of the world-class and call its say­_hello method to print out a message.

Four Pillar of Python OOPs

The four pillars of Python OOPs (Object-Oriented Programming) are:-

  • EncapsulationIt refers to the act of tying together data (variables) and methods (functions) within a class so that they cannot be accessed outside of the class, hence concealing an object's internal information from the outside world. Encapsulation protects data while also making programs easier to maintain and modify.
  • Abstraction: It is the process of hiding the implementation details of a class and exposing only the necessary information to the user. In other words, abstraction allows the user to access only the relevant information and ignores the rest. Abstraction is achieved in Python through abstract classes and interfaces.
  • Inheritance: It is a process by which a new class is created from an existing class, inheriting all the properties and methods of the parent class. Inheritance promotes code reuse, reduces code duplication, and allows us to create more specialized classes.
  • PolymorphismIt refers to an object's ability to change shape. Polymorphism is achieved in Python using method overriding and overloading. Method overriding allows a child class to offer its own implementation of a method declared in the parent class, whereas method overloading allows many methods with the same name However, separate parameters must be defined within each class. Polymorphism encourages flexibility and modularity in code design.
What is Class?

In Python, a class is a blueprint or a template for creating objects. It defines a set of attributes and methods that are shared by all instances (objects) of that class.

A class is defined using the class keyword, followed by the name of the class and a colon. Here's an example of a simple Python class:

example2.png

In the above example, we define a MyClass class that has one attribute (name) and one method (say_hello). The __init__ method is a special method that is called when an object of the class is created, and it initializes the object's attributes.

To create an object of the class, we simply call the class as if it were a function, passing any necessary arguments. Here's an example of how to create an object of the MyClass class and call its say_hello method:

example3.png

In the above example, we create an object of the MyClass class with the name "John" and call its say_hello method, which prints out a greeting message.

What is Method in Python OOP?

In Python, a method is a function that is associated with an object. A method is called on an object and it can access the object's data and manipulate it.

To define a method in Python, you need to first create a class. Here is an example of a class with a method:

example4.png

In this example, we have created a class called Dog with two attributes, name and age. We have also defined a method called bark that prints a message to the console.

To call the bark method on a Dog object, we first need to create an instance of the Dog class:

example5.png

In this example, we have created a Dog object called my_dog with the name "Fido" and age 3. We then call the bark method on my_dog, which prints "Fido says woof!" to the console. 

That is the basics of creating and calling methods in Python. Of course, methods can be more complex and can do a lot more than just print messages to the console!

Methods are divided into three parts:-
  • Parameterized Method
  • Non-Parameterized Method
  • Constructor
Furthermore, Constructor are also divided into two parts and those are: -
  • Parameterized Constructor
  • Non-Parameterized Constructor
1) Parameterized Method

In Python, a parameterized method is a method that accepts one or more parameters, also known as arguments. These parameters allow you to pass data to the method so that it can perform its tasks. Here's an example of a parameterized method:

example6.png

In this example, we've defined a class called Calculator with a method called add. This method takes two parameters, num1 and num2, and returns their sum. 

To call this method, we first need to create an instance of the Calculator class:

example7.png

We can then call the add method on my_calculator and pass in two arguments:

example8.png

In this example, we've called the add method on my_calculator with num1 set to 3 and num2 set to 5. The method returns their sum, which is 8.

You can define methods with as many parameters as you need, and you can use different types of parameters, such as required parameters, default parameters, and variable-length parameters. Here's an example of a method with a default parameter:-

example9.png

In this example, we've defined a class called Greeter with a method called say_hello. This method takes one parameter, name, which has a default value of "World".

To call this method, we can create an instance of the Greeter class and call the say_hello method with or without an argument:

example10.png



In this example, we've created an instance of the Greeter class called my_greeter and called the say_hello method twice. The first time, we didn't pass in an argument, so the default value of "World" was used. The second time, we passed in the argument "Alice", so that value was used instead.
 

Parameterized methods are a powerful feature of Python that allows you to write more flexible and reusable code. By passing in parameters, you can create methods that can work with different data and perform different tasks depending on the input.

2) Non-Parameterized Method

A non-parameterized method in Python is a method that doesn't take any parameters or arguments. It is also called a method with zero arguments. 

Here's an example of a class with a non-parameterized method:

example11.png

In this example, we've defined a class called MyClass. The my_method method doesn't take any arguments and just prints "Hello, world!" to the console.

To call the my_method method on an instance of the MyClass class, we first need to create an instance of the class:

example12.png

This creates an object called my_instance that is an instance of the MyClass class. We can then call the my_method method on my_instance like this:

example13.png

This will call the my_method method on my_instance and print "Hello, world!" to the console. 

Non-parameterized methods are useful when you need to perform a specific action or operation that doesn't require any input from the user. They can be used for initialization, setting default values, or performing some action that is specific to the object or class.

3) Parameterized Constructor

  • Parameterized Constructor:- 
In Python, a parameterized constructor is a special method that gets called when an object is created and accepts arguments that are used to initialize the object's attributes. Unlike the default constructor, which takes no arguments, the parameterized constructor allows you to create objects with different initial values

To define a parameterized constructor in Python, you use the __init__ method and pass the parameters you want to accept as arguments. Here's an example:

example14.png

In this example, we've defined a Person class with a parameterized constructor that accepts two arguments, name and age. The constructor initializes the object's name and age attributes using the arguments. 

To create an instance of the Person class using the parameterized constructor, you pass the arguments to the class when you create the object:

example15.png

In this example, we've created two Person objects called person1 and person2 with different values for the name and age attributes.

You can also define default values for the constructor parameters. If a value is not provided when an object is created, the default value is used. Here's an example:

example16,png

In this example, we've defined a Person class with a parameterized constructor that has default values for the name and age parameters. If no values are provided when an object is created, the default values are used.

Parameterized constructors are a powerful feature of Python classes that allow you to create objects with different initial values. By accepting arguments and initializing the object's attributes, you can create more flexible and reusable code.


Non-Parameterized Constructor

In Python, a constructor is a special method that gets called when an object is created. It is used to initialize the object with default values or values passed as arguments.

A non-parameterized constructor is a constructor that takes no arguments. It initializes the object with default values or sets the values of the object's attributes directly in the constructor.

Here's an example of a non-parameterized constructor in Python:

example17.png

In this example, we've defined a class called Car with a non-parameterized constructor. The constructor initializes the make, model, and year attributes with default values.

To create an instance of the Car class using the non-parameterized constructor, we use the following syntax:

example18.png

This creates an object called my_car with the make "Toyota", model "Corolla", and year 2022.

We can access these attributes using dot notation:

example19.png

In this example, we've accessed the make, model, and year attributes of the my_car object using dot notation.

In summary, a non-parameterized constructor is a constructor that takes no arguments. It initializes the object with default values or sets the values of the object's attributes directly in the constructor.

Object

In Python, an object is an instance of a class. A class defines a blueprint for creating objects with certain properties and methods. When you create an instance of a class, you create an object that has the same properties and methods as the class.

Here is an example of a simple class in Python:
example20.png

In this example, we've defined a class called Person. The __init__ method is a special method that gets called when an object is created. It initializes the object with the name and age attributes.

To create an instance of the Person class, we use the following syntax:

example21.png

This creates an object called person1 with the name "John" and age 25. We can access these attributes using dot notation:

example22.png

We can also create multiple instances of the Person class with different values for the name and age attributes:

example23.png

In this example, we've created another Person object called person2 with the name "Jane" and age 30.

In summary, an object is an instance of a class. When you create an instance of a class, you create an object with the same properties and methods as the class.


CONCLUSION

Classes, methods, and objects are fundamental concepts in object-oriented programming, and they allow you to write more modular and reusable code. By organizing your code into classes and objects, you can create abstractions that hide implementation details and make your code easier to understand and maintain.



so that's it for today, follow us for learn more....

keep learning.....

No comments:

Post a Comment