Chapter 10 Flashcards
What is an object?
An object is a software entity that contains both data and procedures.
What is encapsulation?
Encapsulation is the combining of data and code into a single object.
Why is an object’s internal data usually hidden from outside code?
The data is protected from accidental corruption.
In addition, the programming code outside the object does not need to know about the format or internal structure of the object’s data.
What are public methods? What are private methods?
Public methods can be accessed by entities outside the object.
Private methods cannot be accessed by entities outside the object. They are designed to be accessed internally.
You hear someone make the following comment: “A blueprint is a design for a house. A carpenter can use the blueprint to build the house. If the carpenter wishes, he or she can build several identical houses from the same blueprint.” Think of this as a metaphor for classes and objects. Does the blueprint represent a class, or does it represent an object?
The metaphor of a blueprint represents a class.
In this chapter, we use the metaphor of a cookie cutter and cookies that are made from the cookie cutter to describe classes and objects. In this metaphor, are objects the cookie cutter, or the cookies?
Objects are the cookies.
What is the purpose of the _ init _ method? When does it execute?
Its purpose is to initialize an object’s data attributes. It executes immediately after the object is created.
What is the purpose of the self parameter in a method?
When a method executes, it must have a way of knowing which object’s data attributes it is supposed to operate on. That’s where the self parameter comes in. When a method is called, Python automatically makes its self parameter reference the specific object that the method is supposed to operate on.
In a Python class, how do you hide an attribute from code outside the class?
By starting the attribute’s name with two underscores
What is the purpose of the _ str _ method?
It returns a string representation of the object.
How do you call the _ str _ method?
By passing the object to the built-in str method
What is an instance attribute?
An attribute that belongs to a specific instance of a class
A program creates 10 instances of the Coin class. How many _ _sideup attributes exist in memory?
10
What is an accessor method? What is a mutator method?
Accessor method (getter) returns a value from a class’s attribute but does not change it
Mutator method(setter) stores a value in a data attribute or changes the value of a data attribute in some other way
The typical UML diagram for a class has three sections. What appears in these three sections?
The top section is where you write the name of the class.
The middle section holds a list of the class’s fields.
The bottom section holds a list of the class’s methods.