What Is Encapsulation In Python?|What is Python?|What Is Encapsulation?| How to declare and access data?|What Is Encapsulation In Python?
What Is Encapsulation In Python?
Description:
WELCOME TO OUR NEW
INFORMATIVE POST, WHERE WE LEARN TECHNICAL INFORMATION AND SOLVE USUAL ERRORS
& PROBLEMS.
This tutorial will discover what Python is and the Python Encapsulation idea. Then, we fix some run time issues in coding for deep understanding.
Before we get late, let's get started
What is Python:
Python is the most utilized language in the history of programming languages. It takes part in various types of programming.
Such as website & software building, data analysis, game developing concepts, and automated work.
For this reason, Python is known as a general-purpose language, with the approach of Python, anyone may create a variety of programs.
What Is Encapsulation?
In C++ & Java, one of the essential components of object-oriented programming is Encapsulation.
It implies grouping data elements and operations inside of one class.
Encapsulation aims to
ensure that "sensitive" data is hidden from users.
How to declare and access data?
- Class variables & attributes are declared in
private
- To access the private data, use Get & Set method to
update the private members of a variable.
Program is based on a company profit, where the class is a company , the method is private and the variable is profit
class
Company {
private:
int profit;
public:
void setProfit(int p) {
profit = p;
}
int getProfit() {
return profit;
}
};
int
main() {
Company myObj;
myObj.setProfit(50000);
cout << myObj.getProfit()<<"=
profit of month";
return 0;
}
Output:
50000 = profit of mouth
In the above article, we understand what’s Python & Encapsulation, now we came on our main topic, what is Encapsulation in python.
What Is Encapsulation In Python?
Python encapsulation is an important characteristic of object-oriented programming (OOP).
Its main work is to hide "sensitive "information from public members. It defines the concept of data covering and the methods that work on data within a separate unit.
Every object-oriented programming language
will use the same concept of Encapsulation.
- JAVA
- C++
- Python
These are the major object-oriented programming languages that use the concepts of Encapsulation.
Example code:
# Creating a Base class
class Base:
def __init__(self):
self.a
= "Python
program"
self.__c
= "
Python WEB"
# Creating a derived class
class Derived(Base):
def __init__(self):
#
Calling constructor of
#
Base class
Base.__init__(self)
print("Calling
private member of base class: ")
print(self.__c)
obj1 = Base()
print(obj1.a)
Output:
Python program
Conclusion:
In this article, we will learn new concepts of Python and
Encapsulation.
Then, we perform a small task of Encapsulation program,
which help's to understand their complex concept.
Last, we discussed and coded our main module, what is Encapsulation
in Python?
If you want to learn the latest technical programming, keep
reading our articles.
Have a good day to you.
Comments
Post a Comment