Chapter 4 - Methods and Encapsulation Flashcards

1
Q

Access Modifiers

(4)

A
  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 subclasses. You’ll learn about subclasses in Chapter 5.
  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 modi er.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Optional Specifiers

A
  1. static Used for class methods.
  2. abstract Used when not providing a method body.
  3. final Used when a method is not allowed to be overridden by a subclass.
  4. synchronized On the OCP but not the OCA exam.
  5. native Not on the OCA or OCP exam. Used when interacting with code written in another language such as C++.

strictfp Not on the OCA or OCP exam. Used for making oating-point calcu- lations portable.

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

Varargs in Methods

(2)

A

A vararg parameter must be the last element in a method’s parameter list. This implies you are only allowed to have one vararg parameter per method.

public static void walk(int start, int… nums) { System.out.println(nums.length);
}

–> walk(3) will output 0
(Java creates an array of lenghth 0 for int… nums)

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

Applying Access Modifiers

(4)

A
  1. Private Access: Only code in the same class can call private methods or access private fields.
  2. Default (Package Private) Access: When there is no access modi er, Java uses the default, which is package private access. This means that the member is “private” to classes in the same package. In other words, only classes in the package may access it.
  3. Protected Access: allows everything that default (package private) access allows and more. The protected access modifier adds the ability to access members of a parent class.
    • If a member is used without referring to a variable, we are taking advantage of inheritance and protected access is allowed.
    • If a member is used through a variable, the rules for the reference type of the variable are what matter. If it is a subclass, protected access is allowed.
      * Public Access: public means anyone can access the member from anywhere.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Reasons for static Methods

(2)

A
  • For utility or helper methods that don’t require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.
  • For state that is shared by all instances of a class, like a counter. All instances must share the same state. Methods that merely use that state should be static as well.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Static vs. Instance

(2)

A
* Koala k = new Koala();
 System.out.println(k.count); // outputs count
 k = null;
 System.out.println(k.count); //still outputs count
* A static member cannot call an instance member.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Static Imports

A

import static package.class.methodName;

import static package.class.variableName;

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

Overloading Methods

(2)

A
  • Method overloading occurs when there are different method signatures with the same name but different type parameters.
  • This means there can be different access modifiers, specifiers (like static), return types, and exception lists.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Order for Overloaded Methods

(4)

A
  1. Exact match by type: public String glide(int i, int j) {}
  2. Larger primitive type: public String glide(long i, long j) {}
  3. Autoboxed type: public String glide(Integer i, Integer j) {}
  4. Varargs: public String glide(int… nums) {}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Order of Initialization

A
  1. If there is a superclass, initialize it first
  2. Static variable declarations and static initializers in the order they appear in the file.
  3. Instance variable declarations and instance initializers in the order they appear in the file.
  4. The constructor.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Encapsulation

(5)

A
  1. Properties are private: private int numEggs;
  2. Getter methods begin with is if the property is a boolean.public boolean isHappy() { return happy;}
  3. Getter methods begin with get if the property is not a boolean. public int getNumEggs() { return numEggs;}
  4. Setter methods begin with set. public void setHappy(boolean happy) { this.happy = happy;}
  5. The method name must have a prefix of set/get/is, followed by the first letter of the property in uppercase, fol- lowed by the rest of the property name.public void setNumEggs(int num) { numEggs = num;}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Immutable Classes

(3)

A
  • private Variables
  • no Setters
  • Initialization e.g. in Constructors
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Lambda Syntax

A
  • a -> a.canHop()
  • (Animal a) -> { return a.canHop(); }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Accessing Variables in Lamdas

A
  • Instance and static variables are okay.
  • Method parameters and local variables are fine if they are not assigned new values.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Predicates

A

public interface Predicate<t> {<br></br> boolean test(T t);<br></br>} </t>

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