Software Access Modifiers Flashcards

1
Q

What are Software Access Modifiers (SAMs)

A
  • They are keywords in object oriented languages: C++, Java, Python etc.
  • These set the accessibility of classes, methods and other members
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the 3 access modifiers

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

Level Access Modifiers

A

Top Level - can use public
Member Level - can use public, private and protected

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

Top Level

A
  • Declared with public access modifiers: Visible to all classes everywhere
  • Declared with no access modifier: Visible only to classes within its own package
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Member level

A

no access and public same as Top level

-Class member declared with private access modifier: can only
be accessed by its own class
-
Class member declared with protected access modifier: can only be accessed by classes in its own package and subclasses in another package

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

Class

A

Always has access to its own members

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

Package

A

Can only access public, protected and package-private (no modifier) members

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

Subclass

A

can only access public and protected members

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

World

A

can only access public members

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

How do Access levels affect programming

A
  1. Classes coming from another source- the access levels determine which members of those classes your own classes can use
  2. When writing a class - you need to decide what access level every member variable and every method in your class should have
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why use access modifiers

A

Security viewpoint - encapsulation stops class members from being changed in an uncontrollable way
Programming viewpoint - changes made to a public class variable could mean changing the code everywhere that uses that variable

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

Choosing Access Modifiers

A
  • Use the most restrictive access level that makes sense for a particular class member
  • Use private unless you have a good reason not too
  • Avoid public fields except for constants which cannot be changed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Immutable objects

A
  • It is an object whose internal state remains constant after it has been created.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly