encapsulation Flashcards
how does public, protected, and private work in c++?
public: visible to any user of the class’ object
private: only visible to the class’ own member functions
protected: visible in class’ member functions and member functions of subclasses of the class
difference between private and protected in c++, why not protect everything?
a subclass could accidentally modify a variable in the superclass, since protected gives a lot of power.
to fix this, make the variable private in the superclass, but a protected method that the subclass can access
when inheriting from a superclass, whats the difference between public, protected, and private?
class B : public A
class B: private A (no access modifier is equivalent
- the public members of derived class are now private in B
class B : protected A:
all public members of the base class become protected in the derived class
- for any private members, they
what is friendship?
declares name/signature of class/function that allows the class to access the class’ private members and make it a part of the scope
how do we give access to a node’s methods in a genericlist class? what about for a function in that class?
in class node:
friend class GenericList;
friend void GenericList::doStuff(Node*);
- now generic list’s members can access node’s private members
is friendship inherited:
friendship it not inherited, not transitive (mutual), and not reciprocal
what’s java’s version of friendship?
C++ has “include”, java has “import” where we can import packages
how to use a package in java?
package MyPackage.MyClasses.A;
(defines package and gives location)
* location is in directory “MyClassesA”, which is inside another directory “MyPackage”
public class MyClass1{}
class MyClass2{}
- file must be called “MyClass1.java”, since its the public class
for packages, what about the non-public classes?
- intended for use only internally to the package
- other classes in the package know about them and can make instances of them and use their methods
- classes outside package cant use them
how to use a package in directory mypackage also in directory myclassesa?
import MyPackage.MyClassesA.*;
define java’s protection modes:
public: visible anywhere
protected: visible in the class its defined, subclasses, and same package
private: visible only in the class
blank/package access/friendly access/package protection/package private: (no protection info) makes member visible to itself, and same package, but not subcalsses