Declarations and Access Control Flashcards
* Declare Classes & Interfaces. * Develop Interfaces & Abstract Classes. * Use Primitives, Arrays, Enums, & Legal Identifiers. * Use Static Methods, JavaBeans Naming and Var-Args.
Identifiers can begin with what characters?
A letter, an underscore, or a currency character.
After the first character, identifiers can also include…?
digits.
How long can identifiers be made?
Any length.
JavaBeans methods must be named using what kind of format?
camelCase
Depending on the method’s purpose, JavaBeans methods must start with what?
set, get, is, add, or remove
A ________ code file can have only one public class.
source
(True/False) If the source file contains a public class, the filename cannot match the public class name.
false (it must match)
A file can have only one _______ statement, but multiple imports.
package
The package statement (if any) must be the ________ (non-comment) line in a source file.
first
The ________ statements (if any) must come after the package and before the class declaration.
import
If there is no package statement, what type of statements must be the first (non-comment) statements in the source file?
import
(True/False) Package and import statements apply to some of the classes in the file?
false (applies to all the classes)
Can a file have more than one nonpublic class?
yes
Do files with no public classes have naming restrictions?
no
What are the three access modifiers?
public, protected, and private
What are the four levels of access?
public, protected, default, and private
Classes can have only public or ______ access.
default
(True/False) A class with default access can be seen only by classes within the same package.
true
A class with ______ _______ can be seen by all classes from all packages.
public access
Class visibility revolves around whether code in one class can ____an instance of another class, _____ another class, and ____methods and variables of another class.
create, extend or subclass, access
Classes can also be modified with ______, abstract, or ________.
final…strictfp
Can a class be both final and abstract?
no
What type of class cannot be subclassed?
final
What type of class cannot be instantiated?
abstract
A single abstract method in a class means the _______ class must be abstract.
whole
An _______ class can have both abstract and nonabstract methods.
abstract
The first ________ class to extend an abstract class must implement all of its abstract methods.
concrete
____________ are contracts for what a class can do, but they say nothing about the way in which the class must do it.
interfaces
Interfaces can be implemented by any class, from any __________ ________.
inheritance tree
Are interfaces like 100-percent abstract class, and are implicitly abstract whether you type the abstract modifier in the declaration or not?
Yes
An interface can have only ________ methods, no ______ methods allowed.
abstract, concrete
Interface methods are by default _________ and ______–explicit declaration of these modifiers is optional.
public, abstract
(True/False) Interfaces can have constants, which are never public, static, or final.
False, they are always public, static, and final
Interface constant declarations of public, static, and final are ________ in any combination.
optional
A class implementing an interface can itself be _______.
abstract
An abstract implementing class does/does not have to implement the interface methods (but the first concrete subclass must.
does not
A class can/cannot extend one class, but it can/cannot implement many interfaces.
can, can
Can interfaces extend one or more other interfaces?
yes
(True/False) Interfaces cannot extend a class, or implement a class or interface.
true
Methods and instance (nonlocal) variables are known as ________?
members
Members can use all four access levels. What are those levels?
public, protected, default, and private
What are the two forms of member access?
Code in one class can access a member of another class.
A subclass can inherit a member of its superclass.
(True/False) If a class cannot be accessed, its members can be accessed.
false, its members cannot have access if the class cannot be accessed
What should you determine before determining member visibility?
class visibility
________ members can be accessed by all other classes, even in other packages.
public
If a superclass member is public, the subclass does/does not inherit it–regardless of package.
does
Members accessed without the ________ operator must belong to the same class.
Dot
“this.” always refers to the ___________ executing object.
currently
this.aMethod () is the same as just invoking ___________.
aMethod ()
(True/False) Private member cannot be accessed by code in the same class.
false, private members can be accessed only by code in the same class
Private members are not visible to subclasses, so private members cannot be ________.
inherited
Default and protected members differ only when ________ are involved.
subclasses
(True/False) Default members can be access only by classes in the same package.
true
________ members can be accessed by all other classes in the same package, plus subclasses regardless of package.
protected
protected = ________ plus kids (kids meaning subclasses)
package
Local (method, automatic, or stack) variable declarations cannot have ______ modifiers.
access
________ is the only modifier available to local variables.
final
Local variables don’t get default values, so they must be ______ before use.
initialized
(True/False) Final methods can be overridden in a subclass.
false, they cannot be overridden in a subclass
Abstract methods are declared, with a signature, a return type, and an optional throws clause, but are not ______.
implemented
Abstract methods end in a _________–no curly braces.
semicolon
Name the three ways to spot a non-abstract method.
(1) The method is not marked abstract
(2) The method has curly braces
(3) The method has code between the curly braces
The first nonabstract (concrete) class to extend an ____________ class must implement all of the _______ class’ __________ methods.
abstract, abstract, abstract
The __________ modifier applies only to methods and code blocks.
synchronized
Synchronized methods can have access control and can also be marked ________.
final
Abstract methods must be implemented by a subclass, so they must be inheritable. For that reason: (Name both reasons)
(1) abstract methods cannot be private
(2) abstract methods cannot be final
The ________ modifier applies only to methods.
native
The ________ modifier applies only to classes and methods.
strictfp
As of Java 5, methods can declare a ____________ that accepts from zero to many arguments.
parameter
A _________ parameter is declared with the syntax type…name; for instance: doStuff (int…x) { }
var-arg
A var-arg method can have only one var-gar _______.
parameter
(True/False) In methods with normal parameters and a var-arg, the var-arg must come last.
true
Instance variable can…
(1) Have any access control (2) Be marked final or transient
Instance variables can’t be __________, _____________, _____________, or __________.
abstract, synchronized, native, strictfp
Is it legal to declare a local variable with the same name as an instance variable (also called “shadowing”)?
yes
What are the three properties of final variables?
(1) final variables cannot be reinitialized once assigned a value (2) final reference variables cannot refer to a different object once the object has been assigned to the final variable (3) final reference variables must be initialized before the constructor completes
(True/False) There is no such thing as a final object.
true–an object reference marked final does not mean the object itself is immutable
The transient modifier applies only to _________ variables.
instance
The volatile __________ applies only to instance variables.
modifier
_________ can hold primitives or objects, but the array itself is always object.
arrays
When you _______ an array, the brackets can be to the left or right of the variable name.
declare
(True/False) It is always legal to include the size of an array in the declaration.
false–it is never legal to include the size of an array in the declaration
An array of object can hold any object that passes the ______ ______ for the declared type of the array.
IS-A test
Static variables and methods are/are not tied to any particular instance of a class.
are not
No classes instances are needed in order to use ________ members of the class
static
________ methods do not have a direct access to non-static members
static
An ______ specifies a list of constant values assigned to a type
enum
An enum is/is not a String or an int.
is not
An enum can be declared outside or inside a class, but not in a ___________.
method
An enum declared outside a class must not be marked _______, _______, _______, _______, or _______.
final, abstract, static, protected, private
Enums can contact constructors, methods, _______, and constant class bodies.
variables
Enum contacts can send ___________ to the enum constructor, using the syntax BIG(8), where the in literal 8 is passed to the enum constructor
arguments
(True/False) Enum constructors can never be invoke directly in code.
true
MyEnum.values () returns….
an array of MyEnum’s values