encapsulation Flashcards

1
Q

how does public, protected, and private work in c++?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

difference between private and protected in c++, why not protect everything?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

when inheriting from a superclass, whats the difference between public, protected, and private?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is friendship?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how do we give access to a node’s methods in a genericlist class? what about for a function in that class?

A

in class node:

friend class GenericList;

friend void GenericList::doStuff(Node*);

  • now generic list’s members can access node’s private members
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

is friendship inherited:

A

friendship it not inherited, not transitive (mutual), and not reciprocal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what’s java’s version of friendship?

A

C++ has “include”, java has “import” where we can import packages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how to use a package in java?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

for packages, what about the non-public classes?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to use a package in directory mypackage also in directory myclassesa?

A

import MyPackage.MyClassesA.*;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

define java’s protection modes:

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly