Java Declarations and Access Control

Created by CByrd17 

Upgrade to
remove ads

What can identifiers begin with?

A letter, an underscore or a currency character. Obj 1.3.

What is different after the first character of an identifier?

A digit may be included. So letter, underscore, currency character and digits. Obj 1.3.

What is the maximum length for an identifier?

No maximum! Obj 1.3.

What naming conventions exist for JavaBeans?

They must be named using CamelCase, and must start with get, set, is, add, or remove. Obj 1.3.

How many public classes can be in a source file?

Just one. But you can have plenty of classes that are not public. Obj 1.1.

What is the correlation between a public class and the name of the source file?

The source file name must be the public class' name with .java appended. Obj 1.1.

How many package statements can a class have?

Just one. Many imports, but only one package statement. Obj 1.1.

Where are import statements found?

Between any package statement and any class definition(s). Obj 1.1.

What comes first in a class file when there is no package statement?

The import statements. Obj 1.1.

Which classes in a file to package and import statements apply to?

All of them. Obj 1.1.

How many non-public classes can a class file have?

As many as you like. Obj 1.1

How must you name a class file with no public classes?

Any way you like. Obj 1.1

What are the access modifiers?

public, private, protected. Obj 1.1

What are the access levels?

public, private, protected, default. Obj 1.1

What access levels can classes have?

public and default. Obj 1.1

What visibility does a default access class have?

package level visibility Obj 1.1

What visibility does a public access class have?

All classes can see. Obj 1.1

What are components of class visibility?

Ability to create an instance of a class.
Ability to extend (subclass) a class.
Ability to access methods and variables of a class.
Obj 1.1

What are the non-access modifiers of a class?

abstract, final, strictfp. Obj 1.2

What is the right way to handle an abstract and final class?

A class can't have both these modifiers.Obj 1.1

What does it mean for a class to be final?

It can't be subclassed.Obj 1.1

What does it mean for a class to be abstract?

It can't be instantiated.Obj 1.1

How does an abstract method in a class change the class?

Any abstract methods mean that the class must be marked abstract.Obj 1.1

How many of an abstract class' methods must be abstract?

None. An abstract class can have all non-abstract methods, no methods, or a mixture between the abstract and non-abstract methods.Obj 1.1

What must be true of the first concrete class to extend an abstract class?

It must implement all of its abstract methods.Obj 1.1

What is an interface?

A contract for what a class can do, but does not say anything about how the class does it. Obj 1.2

What type of class can implement an interface?

Any class from any inheritance type. Obj 1.2

How do interfaces relate to abstract classes?

An interface is like a 100% abstract class and is implicitly abstract whether or not you put the modifier on it. Obj 1.2

Can an interface have concrete methods?

Nope. Obj 1.2

What modifiers of interface methods are the defaults?

abstract and public. Obj 1.2

What modifiers of interface constants are the defaults?

public, static, and final. Obj 1.2

Do you need to add modifiers to interface constants?

No, by default they are public, static and final. Obj 1.2

What properties does a legal non-abstract class that implements an interface have?

It provides concrete implementations for the interface's methods.
It must follow all legal override rules for the methods it implements.
It must not declare any new checked exceptions for an implementation method.
It must not declare any checked exceptions that are broader than the exceptions declared in the interface method.
It may declare run-time exceptions on any interface method implementation regardless of the interface declaration.
It must maintain the exact signature (allowing for covariant returns) and return types of the methods it implements (but does not have to declare the exceptions of the interface.
Obj 1.2

Can an abstract class implement an interface?

Yes, but the first concrete subclass must implement all abstract methods and all the interface's methods (which are abstract by default). Obj 1.2

Does an abstract class need to implement all an interface's methods when it implements the interface?

No. The method implementations can be deferred to the first concrete subclass. Obj 1.2

How many interfaces can a class implement?

There is no restriction on how many interfaces. A class may only subclass one other class though. Obj 1.2

What are the rules that an interface must follow when extending or implementing other classes or interfaces?

An interface may extend any number of other interfaces, but may not extend a class or implement any interfaces. Obj 1.2

What is the first thing to look at when evaluating code for correctness?

Verify that interface and class definitions are legal. Obj 1.2

What are methods and non-local instance variables of a class called?

Members. Obj 1.3 and 1.4.

What access levels apply to members?

All four; public, protected, default, and private. Obj 1.3 and 1.4.

What are two ways that members can be accessed?

Referenced directly from another class with visibility or from a subclass through inheritance. Obj 1.3 and 1.4.

What is the relationship between class access and member access?

If a class can't be accessed, it's members cannot either. Obj 1.3 and 1.4.

How should you approach determining member visibility?

Determine class visibility first. Obj 1.3 and 1.4.

What classes can access public members?

All classes, even in other packages. Obj 1.3 and 1.4.

How does inheritance of a public member work?

A subclass inherits all public members regardless of package. Obj 1.3 and 1.4.

What must be true of members accessed without the dot operator?

They must be members of the enclosing class. Obj 1.3 and 1.4.

What does the this operator refer to?

The currently executing object. Obj 1.3 and 1.4.

When do you need to use this.methodName()

You don't. this.methodName() is equivalent to methodName(). Obj 1.3 and 1.4.

What has access to private members?

Only code in the same class. Obj 1.3 and 1.4.

How can private members be inherited by a subclass?

They cannot. A subclass does not have access or visibility to private members. Obj 1.3 and 1.4.

How do default and protected access level members differ?

Default members may be accessed only by classes in the same package whereas protected members may be accessed additionally by subclasses (regardless of package). Obj 1.3 and 1.4.

What does protected mean?

Package plus subclasses? Obj 1.3 and 1.4.

How can subclasses outside of a class; package access protected member?

Only through inheritance. They can't use a reference to a superclass instance. Obj 1.3 and 1.4.

What classes in a particular classes package have access to protected members inherited by that particular class?

Only that particular class' subclasses. No other package member classes can access. Obj 1.3 and 1.4.

What access modifiers apply to local variables?

None. Because they are local, they are only accessible in their narrowest context, so access modifiers are not necessary. Obj 1.3

What modifers are available to local variables?

Only final. Obj 1.3

What is the default value for a local variable?

They do not have them. They must be initialized before use or the class will not compile. Obj 1.3

How do you override a final method in a subclass?

final methods cannot be overridden in a subclass. Obj 1.3

What are the components of declaring an abstract method?

A name, a signature, a return type and an optional throws clause. No implementation. Obj 1.3

How does an abstract method declaration end?

With a semicolon. No curly braces. Obj 1.3

What are three ways to spot a non-abstract method?

The method is not marked abstract.
The method has curly braces.
The method has code inside the curly braces.
Obj 1.3

What is required of the first concrete class to extend an abstract class?

It must implement all of the abstract class' abstract methods. Obj 1.3

To what does the synchronized modifier apply?

Methods and code blocks. Obj 1.3

What kind of modifiers can a synchronized method have?

All the access modifiers, final, strictfp. Obj 1.3

What kind of modifiers cannot be applied to an abstract method?

Private and final. Obj 1.3

To what does the native modifier apply?

Only to methods. Obj 1.3

To what does the strictfp modifier apply?

Only to methods and classes. Obj 1.3

What is a var-arg parameter?

A parameter that accepts from zero to many arguments. Obj 1.4

What is the syntax for a var-arg parameter declaration?

type... name, for example int... anInt Obj 1.4

How many var-arg parameters can a method have?

Only one, and it must be the last parameter in the signature. Obj 1.4

What modifiers apply to instance variables?

Any access control, final and transient. Not abstract, synchronized, native or strictfp. Obj 1.3

Is it legal to declare a local variable with the same name as an instance variable?

Yes, this is called "shadowing". Obj 1.3

What are the properties of a final variable?

It cannot be reinitialized once assigned a value.
If it is a reference variable, it cannot refer to a different object once the object has been assigned to it.
If it is a reference variable, it must be initialized before the constructor completes. Otherwise the class will not compile.
Obj 1.3

What is a final object?

There is no such thing. A reference to an object being final does not mean that changes to the object can't be made. Obj 1.3

What type of modifiers only apply to instance variables?

transient and volatile. Obj 1.3

What can an array hold?

Primitives or objects. Obj 1.3

Is an array a primitive type?

No, it's always an object; even when it holds primitives. Obj 1.3

Where must the brackets be when you declare an array?

They need to be after the type, but can be to the left or the right of the name. int [] ints; and int ints []; are both fine. Obj 1.3

Can you specify the size of the array when you declare it?

No int[5] ints; is NOT legal. Obj 1.3

What can you put into an array of objects?

Any object that is of the type or subtype of the array's type. Obj 1.3

To which instance of a class are static variables and methods tied?

None. They exist without any instantiation and can be referenced using the class name. Like MyClass.myStaticMember or MyClass.myStaticMethod() Obj 1.4

How many copies of a static variable exist at once?

Only one and all instances can modify it. Obj 1.4

What kind of access do static methods have to non-static members?

They do not have direct access. Obj 1.4

What does an enum do?

Specifies a list of constant values assigned to a type.

What type is an enum constant?

Whatever the type of the enum is. So SUMMER or FALL are type Season, not String or int.

Where can an enum be defined?

Inside or outside a class, but never inside a method.

An enum declared outside a class may not be modified in certain ways, what are they?

This type of enum may not be marked static, final, abstract, protected or private.

Enums can contain what features of regular classes?

Constructors, methods, variables, and constant class bodies.

What syntax does an enum constant use to send arguments to a constructor?

Something like BIG(8) where the int literal 8 is passed to the enum constructor in whatever enum BIG is part of.

How can an enum constructor be called from a regular class?

It cannot. These constructors are only called automatically when an enum is initialized.

What must the end of an enum declaration look like?

Either } or }; is acceptable. AKA the semicolon is optional.

How do you get an array of an enum's values called MyEnum?

Like this: MyEnum.values(). This works for all enums, of course. Remember that it returns an array of the enum's type. So for enum Days = { MON, TUE, WED}; Days.values() returns an array of Days not Strings or ints.

When is it appropriate to use the "add" prefix for JavaBeans?

When the method is adding a Listener only.

If variables have default or protected access level, what can a class in another package see?

A class in another package that doesn't subclass the first class can see neither. One that does subclass the first class will be able to see the protected member variable only.

If a super class implements an interface and a subclass also marks the interface implemented, what is required of the subclass?

Nothing. All subclasses of a class that correctly implements an interface also implement the interface, whether they say so or not. If they say so, it's also fine, but they do not also have to implement the interface's methods.

Please allow access to your computer’s microphone to use Voice Recording.

Having trouble? Click here for help.

We can’t access your microphone!

Click the icon above to update your browser permissions above and try again

Example:

Reload the page to try again!

Reload

Press Cmd-0 to reset your zoom

Press Ctrl-0 to reset your zoom

It looks like your browser might be zoomed in or out. Your browser needs to be zoomed to a normal size to record audio.

Please upgrade Flash or install Chrome
to use Voice Recording.

For more help, see our troubleshooting page.

Your microphone is muted

For help fixing this issue, see this FAQ.

Star this term

You can study starred terms together

NEW! Voice Recording

Create Set