4 Access modifiers Flashcards

1
Q

Name and describe the four access modifiers.

A

Java offers four choices of access modifier:

1 -public The method can be called from any class.

2-private The method can only be called from within the same class.

3-protected The method can only be called from classes in the same package or by a SUBCLASS REFERENCE when outside the package. The parent cannot call it’s own method when outside the package.

4- Default (Package Private) Access The method can only be called from classes in the same
package. This one is tricky because there is no keyword for default access. You simply omit
the access modifier.

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

When is the keyword default used in code?

A

There’s a default keyword in Java. You saw it in the switch statement in
Chapter 2, “Operators and Statements,” and you’ll see it again in the next
chapter when we discuss interfaces. It’s not used for access control.

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

What is the order of the method signature?

A
  • access modifier and optional specifier (either of these can be first)
  • return type
  • method name
  • parenthesis parameter type, if any
  • exceptions if any
  • braces - method body
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Name and describe the three optional specifiers on the OCA exam.

A

static: Used for class level methods.
abstract: Used when not providing a method body. Cannot be used until instantiated.
final: Used when a method is not allowed to be overridden by a subclass.

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

What does the method body need to have in regards to the return type?

A

When checking return types, you also have to look inside the method body. Methods
with a return type other than void are required to have a return statement inside the
method body. This return statement must include the primitive or object to be returned.
Methods that have a return type of void are permitted to have a return statement with no
value returned or omit the return statement entirely.

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

What questions should be asked when checking a method for accurate syntax?

A
  • Is the return type there?
  • Is the same return type in the method body?
  • Will that value be returned when the method executes? (return cant be in an if statement)
  • Is the return type next to the method name?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does private access mean?

A
Private access is easy. Only code in the same class can call private methods or access
private fields.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the static & instance rules?

A

Type Calling Legal? How?
Static method Another static method or variable Yes Using the classname

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