What is OOPs in Python?
OOPs stands for object oriented programming.
This is a feature/concept which is generally provided by most of the languages.
This concept is helps to make real-world applications using python.
OOPs helps in making a code so smaller and writing the resuable code.
OOPs is a programming concept that uses some other technical terms to work and these technical terms are necessary to understand before one starts using oops concepts. Technical terms are like–
- class
- object
- method
- inheritance
- polymorphism
- encapsulation
- data-abstraction
We will try to cover everything in detail with easy explanations.
Class
Class is called as the blueprint for the object. If you want to create a class then you need to use class keyword then write the name of the class and finally give a colon(:).
Syntax:
class ClassName: statemen-1 statemen-2 statemen-3 . . . statement-n
A class contains class attributes, class constructor, class methods, etc.
For example, Parrot is an object of class bird that can have attributes like name, age, color, etc.
Object
Objects are the instantiation of classes and also the way to use classes.
These are those entities that have attributes and behaviors which are defined inside the class.
In real-word, object examples are desktop, mobile, ac, freeze, etc.
When we create a class, then we only describe the blueprint for objects but memory is allocated when we create objects of classes.
Syntax
class ClassName: pass obj = ClassName()
Here, ClassName is the name of the class, and obj is the object of the class.
Example
class Employee: # class attribute company = "copyassignment.com" # constructor def __init__(self, name, age, salary): # instance attributes self.name = name self.age = age self.salary = salary # creating objects emp1 = Employee("John", 34, 50000) emp2 = Employee("Harry", 30, 60000) # accessing class attributes using __class__ method # syntax is-- instance.__class__.attribute print(f"{emp1.name} and {emp2.name} work for {emp1.__class__.company}") # accessing instance attributes # syntax is-- instance.instance_attribute print(f"{emp1.name}'s age is {emp1.age} and salary is {emp1.salary}") print(f"{emp2.name}'s age is {emp2.age} and salary is {emp2.salary}")
Output
John and Harry work for copyassignment.com John's age is 34 and salary is 50000 Harry's age is 30 and salary is 60000
Explanation
Class Attribute
We create a class with name “Employee“. Variables that we create in a class are called class attributes and here, the company is a class attribute.
Constructor
Then, we have created the constructor. A constructor is a special method in a class that the python always calls when we create or instantiate an object. Python uses “__init__()” to create a constructor.
The self parameter
The __init__() method can take parameters but the first parameter should always be the “self” parameter which refers to the current instance of the class.
You can use any name instead of self but it should be the first parameter always.
Instance Attribute
Variables that we create inside a constructor are called instance attributes. These attributes are different for different instances. Like here name, age, and salary are different for both emp1 and emp2 instances.
Methods
Methods are the functions that are defined inside the body of a class and associated with an object. They are used to define the behaviors of an object.
Example
class Employee: # class attribute company = "copyassignment.com" # constructor def __init__(self, name, age, salary): # instance attributes self.name = name self.age = age self.salary = salary # class method def myMethod1(self): print(f"Hi {self.name}!") def myMethod2(self, city): print(f"{self.name}, do you lives in {city}?") # creating objects emp1 = Employee("John", 34, 50000) emp2 = Employee("Harry", 30, 60000) # calling methods emp1.myMethod1() emp1.myMethod2("London")
Output
Hi John! John, do you lives in London?
Here, we have created two methods mymethod1 and mymethod2, we can use self parameter with them.
Also Read:
10 Tkinter Projects for Beginners
Introduction Python has become one of the fastest-growing programming languages, and its applications are also increasing every day. Tkinter Projects for Beginners are one of the best ways to learn the basics of Python. Tkinter is said to be one of the most straightforward modules to start learning Python as well. This will help absolute…
10 Useful Python One-Liners
In this tutorial, we will learn about the best and most powerful Python One-Liners ranging from beginner’s level to advanced level that too with examples for better understanding. Using Python One-Liners in your programs will actually make your code more readable and precise. What exactly are Python One-Liners? One-liners in simple language are single-line codes…
100+ Java Projects for Beginners 2023
Are you looking for interesting Java Projects ideas and 100+ Java Projects for beginners with source code? If yes, the you have come to right place because in this article we will show you best 100+ Java project ideas and topics for beginners with source code. We recommend you bookmark this page before you lost it as…
15 Common Coding Mistakes by Beginners
The most common mistakes that beginners while learning to code are 1. Switching between multiple languages 2. No roadmap 3. Lone wolf programming
15 Deep Learning Projects for Final year
Introduction In this tutorial, we are going to learn about Deep Learning Projects for Final year students. It contains all the beginner, intermediate and advanced level project ideas as well as an understanding of what is deep learning and the applications of deep learning. What is Deep Learning? Deep learning is basically the subset of…
20 Python Projects for Resume
About 20 Python Projects for Resume Today, in this article on 20 Python Projects for Resume, we’ll expose you to some of the top beginners to advanced python projects for resumes. In 2022, your best investment will be learning Python through practical projects. Therefore, working on actual Python projects is essential if you wish to…