Encapsulation Flashcards
What is information hiding?
In a C++ class, what does public:, private:, protected: mean?
In C++, what is the difference between private and protected? Why use one over the other?
This is an example of the incorrect way to code: What would be the proper way to isolate what should be class A’s own data member?
Mutators should generally not be just assigning a value. What should they be doing in addition to?
When defining a subclass, what do the following do?
- class B : public A
- class B : private A
- class B: protected A
How would you take the following code, and modify it with inheritance permissions to make a proper stack?
Note: this is still not a good place to use inheritance, the type is not substitutable: A stack is not a subtype of list and can’t serve as a substitute for a list. The correct way to do this would be to use containment(i.e. build the list, then use it as a data member inside a stack).
Consider this code, what’s a better way to code this using iteration?
Make theItem, link public in Node?
- works but defeats the purpose of encapsulation (anybody could use them then).
Make theItem, link protected in node?
- Subclasses could access them then!
- but that doesn’t help, because GenericList is NOT a subclass of Node
SOLUTION:
- Friendship!
C++ friends
- In general how does the friend statement work?
- What does the friend satement allow?
- A friend statement notes the name/signature of a class/function that is meant to have a special relationship to this class
- Allows that class/function access to the class’ private member and makes it part of the object’s scope (allowing it to use the object’s members!)
Provide a code example in a class using friend
In general, how do you achieve friendship (from C++) in Java?
In general, what is a Java package and how does it work?
What is the specific code syntax for packages in Java?
In the following code:
- What is the name of the package?
- What is it’s directory and where must it reside?
How would you add another file to the package attached?
In the following code, what does the public and non-public mean?