Chapter 4 - Methods and Encapsulation Flashcards
Access Modifiers
(4)
- public The method can be called from any class.
- private The method can only be called from within the same class.
- protected The method can only be called from classes in the same package or subclasses. You’ll learn about subclasses in Chapter 5.
- 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.
Optional Specifiers
- static Used for class methods.
- abstract Used when not providing a method body.
- final Used when a method is not allowed to be overridden by a subclass.
- synchronized On the OCP but not the OCA exam.
- 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.
Varargs in Methods
(2)
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)
Applying Access Modifiers
(4)
- Private Access: Only code in the same class can call private methods or access private fields.
- 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.
-
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.
Reasons for static Methods
(2)
- 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.
Static vs. Instance
(2)
* 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.
Static Imports
import static package.class.methodName;
import static package.class.variableName;
Overloading Methods
(2)
- 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.
Order for Overloaded Methods
(4)
- Exact match by type: public String glide(int i, int j) {}
- Larger primitive type: public String glide(long i, long j) {}
- Autoboxed type: public String glide(Integer i, Integer j) {}
- Varargs: public String glide(int… nums) {}
Order of Initialization
- If there is a superclass, initialize it first
- Static variable declarations and static initializers in the order they appear in the file.
- Instance variable declarations and instance initializers in the order they appear in the file.
- The constructor.
Encapsulation
(5)
- Properties are private: private int numEggs;
- Getter methods begin with is if the property is a boolean.public boolean isHappy() { return happy;}
- Getter methods begin with get if the property is not a boolean. public int getNumEggs() { return numEggs;}
- Setter methods begin with set. public void setHappy(boolean happy) { this.happy = happy;}
- 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;}
Immutable Classes
(3)
- private Variables
- no Setters
- Initialization e.g. in Constructors
Lambda Syntax
- a -> a.canHop()
- (Animal a) -> { return a.canHop(); }
Accessing Variables in Lamdas
- Instance and static variables are okay.
- Method parameters and local variables are fine if they are not assigned new values.
Predicates
public interface Predicate<t> {<br></br> boolean test(T t);<br></br>} </t>