Member Modifiers (access and nonaccess) Flashcards
Learn all the different modifiers that can be applied to members of classes.
What’s the access modifier if you don’t type anything before a class or member?
default, which means, package level.
public class Parent { int x = 9; } What is the access modifier for x?
default, which means, package level.
True or False: The subclass Child in a different package from the super class Parent can see the default super class members.
FALSE
When is visible a default member to a subclass?
When the subclass is in the same package than the superclass.
Can access modifiers applied to local variables?
NO
Which is the only modifier that can be applied to a local variable?
final
Which are the member non access modifiers of Java?
static, transient, synchronized, native, strictfp, final and abstract
What does final do to a method?
I prevents the method from being overriden in a subclass and is often used to enforce the API functionality of a method.
Does the code compile?
class SuperClass { public final void show () { System.out.println ("Anything"); } } class Subclass { public void show () { System.out.println ("Something else"); } }
No, trying to override the final method in the super class.
What are method arguments?
Are the variable declarations that appear in between the parentheses in a method declaration.
Can a method argument be declared final?
Yes
What is an abstract method?
The method no contains functional code. It ends with semicolon instead of braces, this is, doesn’t have body.
Does the code works?
public class IlegalClass { public abstract void doIt (); }
No, it has an error when compiling because with an abstract method the class must be declared abstract.
Does the code compiles?
public abstract class LegalClass { void goodMethod ( ) { // working code } }
Yes, an abstract class can have concrete methods.
What does concrete class mean?
That it’s not abstract.
A method can be marked as abstract and final TRUE or FALSE?
FALSE
Can a method be marked as private abstract TRUE or FASLE
FALSE
What’s the opposite modifier to “abstract”?
final
What modifiers prevent that a method may be overriden?
final and private
If the superclass knows all about the implementation of a certain method it means that the method is?
final
What is the error in:
abstract static void doStuff ();
abstract and static cannot be used at the same time
Can abstract be used along with static?
NO
What is a synchronized method?
Indicates that a method can be accessed by only one thread at a time.
The modifier syncrhonized can be applied to which elements?
Only methods.
Which access modifiers can be combined with synchronized?
All of them.
What is used for the “native” modifier.
Indicates that a method is implemented in platform-dependent code, often in C.
Can native be applied to methods, variables and classes?
No, only methods.
What should be the methods body if it’s marked as native?
semicolon (;) like abstract.
strictfp modifier can be applied to?
classes and methods.
What implies strictfp modifier?
forces floating point and floating point operations to adhere to the IEEE 754 standard.
Why to mark a class or method with strictfp?
So you can predict how your floating points will be regardless the underlying platform.
A variable can be declared strictfp?
No, only methods and classes.