What is OOPS?
General Definition
Object-oriented programming (OOP) is a way of writing computer programs using “objects” to stand for data and methods.
Object-Oriented Programming methodologies:
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction ### Python oops concepts Major OOP (object-oriented programming) concepts in Python include Class, Object, Method, Inheritance, Polymorphism, Data Abstraction, and Encapsulation. ## Classes ### Syntax Class is defined under a “Class” Keyword.
class class1(): // class 1 is the name of the classclass class1(): // class 1 is the name of the classclass class1(): // class 1 is the name of the class
Enter fullscreen mode Exit fullscreen mode
## Objects:
Objects are an instance of a class. It is an entity that has state and behavior. In a nutshell, it is an instance of a class that can access the data
Syntax
obj = class1()obj = class1()obj = class1()
Enter fullscreen mode Exit fullscreen mode
Creating an Object and Class in python:
class employee():def __init__(self,name,age,id,salary): //creating a functionself.name = name // self is an instance of a classself.age = ageself.salary = salaryself.id = idemp1 = employee("harshit",22,1000,1234) //creating objectsemp2 = employee("arjun",23,2000,2234)print(emp1.__dict__)//Prints dictionaryclass employee(): def __init__(self,name,age,id,salary): //creating a function self.name = name // self is an instance of a class self.age = age self.salary = salary self.id = id emp1 = employee("harshit",22,1000,1234) //creating objects emp2 = employee("arjun",23,2000,2234) print(emp1.__dict__)//Prints dictionaryclass employee(): def __init__(self,name,age,id,salary): //creating a function self.name = name // self is an instance of a class self.age = age self.salary = salary self.id = id emp1 = employee("harshit",22,1000,1234) //creating objects emp2 = employee("arjun",23,2000,2234) print(emp1.__dict__)//Prints dictionary
Enter fullscreen mode Exit fullscreen mode
Inheritance
It generally means “inheriting or transfer of characteristics from parent to child class without any modification”. The new class is called the derived/child class and the one from which it is derived is called a parent/base class.
Polymorphism
Polymorphism is one of the core concepts in OOP languages. It describes the concept that different classes can be used with the same interface. Each of these classes can provide its own implementation of the interface. Java supports two kinds of polymorphism. You can overload a method with different sets of parameters.
Encapsulation
Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse
原文链接:OOPS IN PYTHON
暂无评论内容