Chapter 4: Methods and Encapsulation Flashcards
What does a basic method signature (method declaration) include?
access modifier, optional modifiers, return type, name, parameters, method body public static void main( String [] args){ //method body }
Which of these are required? Access modifier (ie Public) specifier (final) Return type Method name Parameter list exception list method body
no no yes yes yes no yes
What are the four access modifiers?
public private protected and default (which is package private, there is actually no keyword for this)
Which of these compile? public void walk1(){}
default void walk2(){}
void public walk3(){}
void walk4(){}
yes
no default is not a keyword
no access modifier comes before the return type
yes
What are the three optional specifiers you need to know for this exam?
final used when a method is not allowed to be overridden by a subclass static when you want to bind a method to a class abstract used when not providing a method body
Which of these don’t compile and why?
public void walk(){}
public final void walk2(){}
public static final void walk3(){}
public final static void walk4(){}
public modifier void walk5(){}
public void final walk6(){}
final public void walk7(){}
yes
yes
yes
yes
no modifier is not a key word
no modifiers cannot come directly before the return type
yes
Which of these compile?
public void walk1(){}
public void walk2(){return;}
public String walk3(){return “”;}
public String walk4(){}
public walk5(){};
String walk6(int a){ if (a==4 return “”;)}
yes
yes
yes
no there is not a return statement in the body
no
no there is no return type specified
no tricky, but because a string may not always be returned, if a does not equal 4 for example, the compiler will complain
Which of these compiles?
int number(){ int temp = 9; return temp; }
int longMethod(){ int temp =9L return temp ; }
The first. You cannot stuff a long into an int. Also, this shows that a method that is supposed to return an int cannot return a long value.
Which of these compile? public void walk1(){} public void 2walk(){} public walk3 void(){} public void Walk_$(){} public void() {}
yes no you cannot start a method name with a number no, the name cannot come before the return type yes no, there is no method name
Is the parameter list required?
Yes, but it doesn’t have to contain any parameters
Which of these compile? public void walk1(){} public void walk2{} public void walk3(int a){} public void walk4(int a; int b){} public void walk5(int a, int b){}
yes no, no parameter list ie the parenthesis are missing yes no, semi-colon in the place of the comma. Semi-colons are for separating statements, no parameter lists yes
How does Java indicate that something went wrong?
By throwing an exception
Which of these compile? public void example1(){} public void example2() throws IllegalArgumentException{} public void example3() throws IllegalArgumentException, InterruptedException{}
All of these compile. You can have as many exceptions as you want–separated by commas–or none at all.
What is a method body?
Simply a code block { } that contains zero or more Java Statements.
Which of these compile? public void walk1(){} public void walk2(); public void walk3(int a ) { int anem = 5;}
yes no, no method body yet there is a semicolon. yes
What does varargs stand for?
variable argument(s) that are treated like an array
How are varargs different than arrays?
Varargs parameters must be the last element in the method’s parameters list.
How many varargs can you have in a method signature?
Only one. Varargs must be the last element of a methods parameter list, therefore, there can only be one of them.
Which of these compile? public void walk1(int… numbers){} public void walk2(int start, int… numbers){} public void walk3(int…numbers, int start){} public void walk4(int… start, int… numbers){}
yes, varargs at the end yes, varargs at the end and separated by a comma no, varargs are not at the end no, you can only have one varargs element in the parameter list of method signature
What are the three ways you can call a method with a varargs parameter?
- You can pass in an array of elements. 2. Or, you can list the elements of the array and Java will create the array for you. 3. Or, you can omit the varargs values in the method call and Java will create an array of zero
What is the output? public static void main(String[] args) { walk(1); walk(1,2); walk(1,2,3); walk(1, new int[] {4,5}); walk(1, null); } public static void walk(int start, int… numbers){ System.out.println(numbers.length); } }
0 1 2 2 null pointer exception. As null is not an int, Java treats it as an array reference that happens to be null. When walk tries to determine the length of the array it returns a null pointer exception.
What is the output? public static void main(String[] args) { run(11, 22); } public static void run(int… numbers) { System.out.println(numbers[1]); } }
- you can access a varargs parameter just like accessing an array.
How accessible are ‘private’ classes, methods and fields?
Only accessible within the same class. Only code in the same class can call private methods or access private fields. Accessing private members of another class will always stop the program from compiling.
How accessible are ‘default’ (package private) access:
private and other classes in the same package. This means that the member is “private” to classes in the same package. In other words, only classes in the package may access it.
How accessible are protected classes, methods and fields?
This does everything the default
How accessible are public classes, methods and fields?
protected and classes in other packages.
Will this compile? public class App { private static int a; public static String b; public static char c; public static void main(String[] args) { boolean[] b = { true, false }; Duck duck = new Duck(); duck.makeNoise(); System.out.println(b.length); } } class Duck { private String noise = “quack”; private void quack() { System.out.println(noise); } private void makeNoise() { quack(); } }
No. The makeNoise() method call will stop the program from compiling. It is in a class that is accessible, but the methods themselves are not accessible because they are marked private. If makeNoise() were marked public or protected, it would compile just fine.
True or False: When there is no access modifier only classes within the same package have access?
True
What’s the difference between default access and protected access?
default provides the ability for classes of the same package to have access. Protected adds the ability for children to access members of a parent class.
What does it mean to extend a super class?
Extending means creating a subclass that has access to any protected or public members of the parent class.
Can you access protected members from a class in a different package?
Yes. But only if the class in question is a child of the parent which belongs to the different class.
What can access a protected method?
A call from the same package or from another class extending the super.
How would you go about accessing a protected method or field from another class? What are the two ways of doing so?
- It’s in the same package. 2. protected methods and fields can be accessed by children.
What will happen if you try to call a protected method from a class/instance of an object that is defined in a different package?
It will work fine if it’s public. Otherwise it won’t work. Unless you have inherited and treated that other class as a super. You can use protected methods from a super.
Why is it that a child class does not have access to the protected methods of a new instance of the parent class within the child’s method body? (assuming the parent is in a different package.)
The new object is NOT the same object as that which the child is inheriting from. So the protected methods essentially become private
What does public access mean?
Anyone can access the public methods, classes, fields from anywhere.
Can you call static methods without an instance of the class?
Yes. Think of the main() method.
Other than the main method, what are two purposes for static methods?
- For utility or helper methods not requiring the state of an object to work well. 2. For state that is shared by all instances of a class, like a counter.
Other than the main method, what are two purposes for static methods?
- For utility or helper methods not requiring the state of an object to work well. 2. For state that is shared by all instances of a class, like a counter.
What is the output? public class App { public static int k; public static void main(String[] args) { System.out.println(k); App app = new App(); System.out.println(app.k); app = null; System.out.println(app.k); } }
0 0 0 It doesn’t matter that app is set to null because k is a static value.
What is the output? public class App { public static int k; public static void main(String[] args) { System.out.println(k); App app1 = new App(); App app2 = new App(); app1.k = 1; app2.k = 2; System.out.println(k); } }
- the app1 and app2 are just distractions from the fact that this is a static variable.
What constitutes a ‘member’ of a class?
a field or a method of a class
What is the output: public class App { private static String name = “Jedd”; public static int k; public static void main(String[] args) { first(); second(); third(); } public static void first() {System.out.println(name);}; public static void second() {System.out.println(name);}; public void third() {System.out.println(name);}; }
This won’t compile. You can’t make as static reference to a non static method. A static member cannot call an instance member. BUT, once you made an instance of the class, you could access that static field.
What is the output? public class App { private String name = “Jedd”; public static int k; public static void main(String[] args) { first(); second(); third(); } public static void first() {System.out.println(name);} public static void second() {System.out.println(name);} public static void third() {System.out.println(name);} }
This won’t compile. You cannot call an instance member from a static member. The only way to fix this would be to make the methods instance methods as well.
If you have a static method, can you call another static member? (member meaning method or field) If yes, how would you do so?
Yes. You can just use the class name. public class App { private static String name = “Jedd”; public static void first() {} public static void second() {} public static void third() {System.out.println(name);} public static void main(String[] args) { App.first(); second(); App.third(); //also works if you just say first, second or third without App in front. Class name is inferred by the compiler } }
It you have a static method, can you call an instance member? If yes, how would you do it?
No. At least not without instantiating an object.
If you have an instance method, can you call a static method or variable? If yes, how would you do it?
By using the class name or a reference variable. For example: public class App { private static String name = “Jedd”; USING REFERENCE VARIABLE public void first() {System.out.println(name);} USING CLASS NAME public void second() {System.out.println(App.name);} public static void main(String[] args) { App app = new App(); app.first(); app.second(); } }