1. What is encapsulation 2. Why should an object\'s dta attributes be hidden fro
ID: 3714350 • Letter: 1
Question
1. What is encapsulation 2. Why should an object's dta attributes be hidden from code outside the class 3. What is the diference between a class and an instance of a class? 4. The following statement calls an object's method. What is the name of the method What is the name of the variable that references the object.? allet.get dol1ar() 5 When the_.init. method executes,what does the self parameter reference 6. In a Python clas, how do you hide an attribute from code outside the class? . How do you call he .str... method?Explanation / Answer
1. Encapsulation - Encapsulation is defined as the mechanism that binds the code together and in encapsulation the wrapping up of data is under a single unit, it is a protective shield that prevents the data from being accessed by the code outside this shield.
2. Object data attributes should be hidden from outside class to prevent the unauthorized access.
3. Class is a blue print in which objects are created it represents the set of properties or methods that are common to all objects of one type and instance of class is objects . Objects hold a state, state are variables which are saved together within an object, we call them fields or member variables.
4. wallet.get_dollar()
The method name is get_dollar() and variable name is not present here it is simply calling a method without passing any parameters.
5._ _init_ _ method is called when an object is created from the class and it allow the class to initialize the attributes of a class.It is a reseved method in python classes. It is known as a constructor in object oriented concepts.
Self Parameter Reference :- self represents the voice of the object.class have methods. Also, a class can have multiple objects. Right? Now when any object calls a method of the class, how would the method know which object has called it?
e.g;if any of your family member calls your name, how do you recognize that its your mother or father or brother, if you are sitting in a different room? You recognize the voice. Right?
so self parameter knows which parameter is called.
self parameter references to the one of the multiple objects being called by recoginizing.
6.In Python, we use double underscore ( __) before the attributes name and those attributes will not be directly visible outside.
7.__str__ is the built-in function in python, used for string representation of object.
The return value must be a string object.
If you have any query related to my solution do comment below and if you like my solution please give a thumbs up!
Thanks :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.