Refers to the languages that uses objects in programming, which can contain data, in the form of fields, and code, in the form of procedures
Fields
Can be thought of as containers for storing information that is used by the object or class
Also known as a data member or member variable
Principles of programming
Data abstraction
Encapsulation
Inheritance
Polymorphism
Dynamic bonding
Message passing
Programming languages that uses OOP
Java
C++
Python
Aim of OOP
Aims to implement real-world entities in programming
To bind together the data and the functions that operate on them so that no other part of the code can access this data except that function
Real-World entities
Is an entity with a physical location (within the universe)
Name all Real-world entities or OOP Concepts
Class
Objects
Data Abstraction
Encapsulation
Inheritance
Polymorphism
Dynamic Bonding
Message Passing
Class
Is a user-defined data type
Is the design or blueprint of any entity, which defines the core properties and functions
The class has 2 central components
Properties
Methods
Class
Is an abstraction of some entities which contains common set of properties and methods
Self
Means the class itself
Constructor
To define and initialize the instance variables
Instance attribute
Refers to the properties of that particular object like edge of the triangle being 3, while the edge of the square can be 4
DataAbstraction
Is used to hide the internal functionality of the function from the users
It is where the users only interact with the basic implementation of the function, but inner working is hidden
Double underscore (____)
We can also perform data hiding by adding the double underscore (____) as a prefix to the attribute which is to be hidden
Encapsulation
In Python is one of the 4 important concepts to understand. The other three are inheritance, polymorphism, and abstraction
Methods to Control Access
Using Single Underscore
Using Double Underscores
Using Getter and Setter methods to access private variables
Using Single Underscore
A common Python programming convention to identify a private variable is by prefixing it with an underscore
Using Double Underscores
If you want to make class members like methods and variables private, then you should prefix them with double underscores
Is useful to define the properties of the class
Init method
Will be called whenever we will create an instance of the class
Objects
Is a specific instance of a class. It is a thing in a real world. It is a thing that you want to store and process data about
Entity
Another name for an object
Is the existence of something considered apart from its properties
Instance of an object
Whenever we create a new class in Python and whenever we create a variable, it automatically creates an
Self-keyword
Is used as the first parameter to a method
Self
Refers to the current object
Instance variables
Are declared inside a method using the self-keyword
Name mangling
But Python offers some sort of support to the private modifier. This mechanism is called
Name mangling- In Python, any identifier with __Var is rewritten by a python interpreter as _Classname__Var, and the class name remains as the present class name
Using Getter and Setter methods to access private variables
If you want to access and change the private variables, accessor (getter) methods and mutators (setter methods) should be used, as they are part of Class
What is the need for Encapsulation in Python
Encapsulation helps in achieving the well-defined interaction in every application
The Object-Oriented concept focuses on the reusability of code in Python. (DRY – Don't Repeat Yourself)
The applications can be securely maintained
It ensures the flexibility of the code through a proper code organization
It promotes a smooth experience for the users without exposing any back-end complexities
It improves the readability of the code. Any changes in one part of the code will not disturb another
Encapsulation ensures data protection and avoids the access of data accidentally. The protected data can be accessed with the methods discussed above
Encapsulation in Python is, the data is hidden outside the object definition. It enables developers to develop user-friendly experience. This is also helpful in securing data from breaches, as the code is highly secured and cannot be accessed by outside sources
Inheritance
Is an important pillar of OOP
The capability of a class to derive properties and characteristics from another class is called
Inheritance allows the user to reuse the code whenever possible and reduce its redundancy
Types of Inheritance
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Single Inheritance
Is a type of inheritance where a derived/child class inherits properties and behaviors from a single parent/base class
MultipleInheritance
Have 2 parent classes and from that 2-parent classes it inherits a single child class and that child class will consist the properties
All of the properties of the parent classes will be inherited by the child class using an object
Pythoninheritance
The property of acquiring all the properties and behaviors of the parent object by an object is termed as
Python facilitates inheritance of a derived class from its base class as well as inheritance of a derived class from another derived class
Multilevelinheritance in Python
The inheritance of a derived class from another derived class is called
HierarchicalInheritance
Is the inheritance with 1 parent class which will be inherited for many child classes