CSC 335 - Encapsulation

0.0(0)
Studied by 3 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/29

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 11:13 PM on 10/4/24
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

30 Terms

1
New cards

Encapsulation

To hide the internal implementation of an abstraction behind an interface that tightly controls how an abstraction can be used

2
New cards

What is the principle of Information Hiding (encapsulation context)?

Means that our encapsulated structures should reveal the least amount of information as possible while still being able to be used as intended

3
New cards

Immutability

The inability for an item to ‘mutate’, or change

4
New cards

Modular Programming

The general programming concept of separating a program into distinct pieces or ‘modules’

5
New cards

Escaping Reference

Concept of private members being publicly available outside the scope of the object

6
New cards

Rule of Narrowest Scope

A general rule of the thumb that relates to giving a field or method the minimum level of visibility required for a program to work as intended

7
New cards

What access modifier should instance variables generally be set to?

Private

8
New cards

What access modifier should methods in an API generally be set to?

Public

9
New cards

What access modifier should helper methods used only within a class be set to?

Private

10
New cards

A class is well encapsulated when it is impossible to change data stored by an object without _________________.

Going through one of its methods

11
New cards
<p>Where is the escaping reference in this code?</p>

Where is the escaping reference in this code?

The setCards(List<Card> pCards> function allows for an externally defined List of cards to be set as the private member of the Deck class, which is an escaping reference because the client code has prior access to this reference

12
New cards
<p>Where is the escaping reference in this code?</p>

Where is the escaping reference in this code?

The constructor allows a user-created List<Card> object to be instantiated as the private aCards field of the Deck class, which is an escaping reference

13
New cards
<p>Where is the escaping reference in this code?</p>

Where is the escaping reference in this code?

This one is tricky: notice that the function asks for a List of Lists, and the method is adding a List private member to this user-supplied List of Lists. Because the List of Lists has direct access to its elements, this is an escaping reference

14
New cards

What are the 2 primary tools for achieving encapsulation and avoiding escaped references?

Accessibility modifiers and immutability

15
New cards

How do immutable objects help achieve encapsulation?

Immutable objects neutralize the threat of escaping references, because they can not be modified anyways

16
New cards
<p>What is the issue with this code?</p>

What is the issue with this code?

The constructor doesn’t stop null values from being passed in as parameters and setting the aRank and aSuit members to null as a result

17
New cards
<p>What is the issue with this code?</p>

What is the issue with this code?

The draw() function does not check if the deck is empty before attempting to remove a card from the list

18
New cards
<p>What is the issue with this code? </p>

What is the issue with this code?

The getCards() methods returns a reference to the private member variable aCards, which is an escaping reference

19
New cards
<p>What is the issue with this code?</p>

What is the issue with this code?

main is able to access the List<Card> aCards private member variable of the Deck class, which is an escaping reference

20
New cards
<p>What is the issue with this code?</p>

What is the issue with this code?

The setCards() function allows client code to supply a List of Card objects as the argument, which is then set to the private member aCards, which is an escaping reference

21
New cards
<p>What is the issue with this code?</p>

What is the issue with this code?

A private member variable is added to a client-code supplied List of Card Lists, which is technically an escaping reference since the user has access to the Lists.

22
New cards
<p>What is the issue with this code?</p>

What is the issue with this code?

In the 4th line the cards List is assigned to the return value of allCards.get(0). This return value is the private data member of the ‘deck’ object created in line 3, which is an escaping reference

23
New cards
<p>Is this class immutable?</p>

Is this class immutable?

Yes, because the fields are set in the constructor (no setters), and cannot be reset

24
New cards

Why is only being able to modify an object through its methods a good idea, besides for encapsulation?

To ensure the object is used correctly, so that client code can not corrupt the fields and/or set them to invalid values

25
New cards
<p>Can the fields here be corrupted?</p>

Can the fields here be corrupted?

Yes, the constructor does not check for null values

26
New cards

What does the unmodifiableList provide?

Returns an unmodifiable view of the list, that does not support operations like adding, removing, or updating elements

27
New cards

What is the idea behind Design by Contract?

The idea that code will produce the right post-conditions if the right pre-conditions are met

28
New cards

What does the assert statement do?

Assert is a Java mechanism for enforcing that code is not used without pre-conditions being met (literally - will raise an AssertionError otherwise)

29
New cards

What are javadocs and what are their use?

Javadocs are a documentation of what the pre-conditions of code are. It is a very lightweight way to Design by Contract, as it does not actually check if the pre-conditions are met but assumes that client code is responsible for using the code correctly.

30
New cards
<p>What is the issue with this code?</p>

What is the issue with this code?

Both javadocs and explicit input validation are being used - only one or the other should be used individually