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?


How would you import the attached java package?

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

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

What should always be part of classpath, so placement inside the current folder should always work?
.(the current folder)
If we don’t define a superclass implementation for a (non-pure) virtual method, we get what type of error?

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

Why use packages in Java?

In Java protection modes, what does Public mean?
Visible to anybody anywhere
In Java protection modes, what does Protected mean?

In Java protection modes, what does Private mean?
Visible only to the class in which it’s defined
In Java protection modes, what does Blank(supply no protection info at all) mean?

What would the folder structure look like for the following code?


For the attached code, can either class access defaultInt? Why?


For the attached code, can either class access privatetInt? Why?


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

What is the difference between Protected and package protection?

For the attached code, why can’t class OutsideTest access x.defaultInt?


For the attached code, in class OutsideTest why can’t ProTest access privateInt?


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)

Why can OutsideSubClass access protectedInt?




In reference to Java and protected, what is worthwhile remembering regarding the underlying implementation?

In general, what is the object pointer?

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);
For the following code, what is p REALLY doing?


In Python, how does self reference work?
