Unit_8: Encapsulation Flashcards
1
Q
Explain public, private protected.
A
- public: visible to any user of a class X object
- private: visible only to X’s own member functions => better security and reliability, and better control of data locally
- protected: visible in X’s member functions and member
functions of subclasses (derived classes) of X
2
Q
compare Private vs Protected
A
we try to isolate functionality in asuperclass – i.e., if the superclass owns a variable they should be defining ways to manipulate it
- This is a general principle you should always follow!
Subclasses should deal only with the things they
are responsible for
3
Q
What is Unique C++ features? Why need it?
A
friend
allows a function, class, or method to access private and protected members of another class
4
Q
Why protected (in Java) insecure?
A
protected members are accessible by subclasses AND classes in the same package
=> should be private, with
protected accessors/mutators that can be inherited by children