Encapsulation Flashcards

1
Q

What is information hiding?

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

In a C++ class, what does public:, private:, protected: mean?

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

In C++, what is the difference between private and protected? Why use one over the other?

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

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?

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

Mutators should generally not be just assigning a value. What should they be doing in addition to?

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

When defining a subclass, what do the following do?

  • class B : public A
  • class B : private A
  • class B: protected A
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you take the following code, and modify it with inheritance permissions to make a proper stack?

A

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).

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

Consider this code, what’s a better way to code this using iteration?

A

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

C++ friends

  1. In general how does the friend statement work?
  2. What does the friend satement allow?
A
  1. A friend statement notes the name/signature of a class/function that is meant to have a special relationship to this class
  2. 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!)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Provide a code example in a class using friend

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

In general, how do you achieve friendship (from C++) in Java?

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

In general, what is a Java package and how does it work?

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

What is the specific code syntax for packages in Java?

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

In the following code:

  1. What is the name of the package?
  2. What is it’s directory and where must it reside?
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How would you add another file to the package attached?

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

In the following code, what does the public and non-public mean?

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

How would you import the attached java package?

A

We can also import only a single class from a package by substituting its name for the wildcard(*)

18
Q

If I am not using any Java packages, we don’t need to care about any of the package details?

19
Q

What should always be part of classpath, so placement inside the current folder should always work?

A

.(the current folder)

20
Q

If we don’t define a superclass implementation for a (non-pure) virtual method, we get what type of error?

21
Q

For a linker error, why can’t C++ catch this error at compile time?

22
Q

Why use packages in Java?

23
Q

In Java protection modes, what does Public mean?

A

Visible to anybody anywhere

24
Q

In Java protection modes, what does Protected mean?

25
In Java protection modes, what does Private mean?
Visible only to the class in which it's defined
26
In Java protection modes, what does Blank(supply no protection info at all) mean?
27
What would the folder structure look like for the following code?
28
For the attached code, can either class access defaultInt? Why?
29
For the attached code, can either class access privatetInt? Why?
30
For the attached code, can either class access protectedInt or publicInt? Why?
This shows you that protected is in fact a lot more insecure than you would at first think. That's why data members shouldn't be protected, as we've already discussed. They should be private, with protected accessors/mutators that can be inherited by children (or blank/friendly, so those in the same package can access!) * and hopefully high level intelligent methods rather than just blunt gets/sets * ... and simulatly, these accessors/mutators could be blank(package-private), to allow anybody in the same package(presumably there for the same purpose!) to use them
31
What is the difference between Protected and package protection?
32
For the attached code, why can't class OutsideTest access x.defaultInt?
33
For the attached code, in class OutsideTest why can't ProTest access privateInt?
34
For the attached code, in class OutsideSubClass why can't it access OtherClass x or defaultInt? Even though it extends ProTest?
the class IS related to ProTest but it's in a different package (default/unamed package)
35
Why can OutsideSubClass access protectedInt?
36
37
In reference to Java and protected, what is worthwhile remembering regarding the underlying implementation?
38
In general, what is the object pointer?
39
For the following code, if you want to invoke dostuff form inside whater, and supply the object itself as an argument, what code would you use?
dostuff(this);
40
For the following code, what is p REALLY doing?
41
In Python, how does self reference work?