Chapter 4 Methods and Encapsulation Flashcards
What access modifiers Java offers for methods ?
- 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.
- 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.
What optional specifiers Java offers (six) ?
- 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 floating-point calculations portable.
What is the difference between default and protected access modifiers from methods point of view ?
- Protected - The method can only be called from classes in the same package or subclasses.
- Default - The method can only be called from classes in the same package.
Can instance members call static members and vice versa ?
When referenced from outside the class, methods are called using the classname—for example, StaticClass.method(). Instance members are allowed to call static members, but static members are not allowed to call instance members.
What is pass-by-value ?
It means that calls to methods create a copy of the parameters. Assigning new values to those parameters in the method doesn’t affect the caller’s variables.
What means overloaded methods ?
Overloaded methods are methods with the same name but a different parameter list. Java calls the most specific method it can find. Exact matches are preferred, followed by wider primitives. After that comes autoboxing and finally varargs.
When the default no-argument constructor is called ?
The default no-argument constructor is called when no constructor is coded.
Can multiple constructors call each other ?
Multiple constructors are allowed and can call each other by writing this(). If this() is present, it must be the first statement in the constructor.
Do you know the 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.
What rules for JavaBeans naming conventions do you know ?
- Properties are private;
- Getter methods begin with is if the property is a boolean;
- Getter methods begin with get if the property is not a boolean;
- Setter methods begin with set;
- The method name must have a prefix of set/get/is, followed by the first
letter of the property in uppercase, followed by the rest of the property name;
Are parentheses around parameter list required in lamda expressions ?
The parentheses are only optional when there is one parameter and it doesn’t have a type declared.
print(() -> true); // 0 parameters
print(a -> a.startsWith(“test”)); // 1 parameter
print((String a) -> a.startsWith(“test”)); // 1 parameter
print((a, b) -> a.startsWith(“test”)); // 2 parameters
print((String a, String b) -> a.startsWith(“test”)); // 2 parameters
Will this compile:
public void methodA () { return; }
public void methodB () { return null; }
public void methodA () { return; } // YES
public void methodB () { return null; } // NO - Void methods cannot return a value
What is encapsulation and what is immutability ? How they differ ?
Encapsulation requires using methods to get and set instance variables so other classes are not directly using them. Instance variables must be private for this to work. Immutability takes this a step further, allowing only getters, so the instance variables do not change state.
Will this compile and if not why:
public class TestClass { public testClass() {} }
It won’t compile. The compiler will say “Return type for the method is missing”. It looks like default constructor but it isn’t because its name should match the name of the class (case-sensitive).
True or False: Both instance and static initializers are able to access static variables ?
True