1. Declarations and Access Control Flashcards
How identifiers can begin, what chars can have after that and what could be their length ?
Identifiers can begin with a letter, an underscore, or a currency character. After the first character, identifiers can also include digits. Identifiers can be of any length.
How you can compile and execute Java programs without IDE ?
You can compile and execute Java programs using the command-line programs javac and java, respectively.
What is the special signature of main() method? Could it be overloaded ? Write three different legal declarations of main() method.
The only versions of main() methods with special powers are those versions with method signatures equivalent to public static void main(String[] args). main() can be overloaded.
static public void main(String[] args)
public static void main(String… x)
static public void main(String bang_a_gong[])
Static imports syntax ?
the syntax is import static…
How many public and nonpublic classes can have a source code file ?
A source code file can have only one public class and more than one nonpublic class.
How many packages and imports can have a source file ?
A file can have only one package statement, but it can have multiple imports.
Order of import, class declaration and package ?
The package statement (if any) must be the first (noncomment) line in a source file. The import statements (if any) must come after the package and before the class declaration. If there is no package statement, import statements must be the first (noncomment) statements in the source file.
How many access modifiers, general/class access levels you know?
There are three access modifiers: public, protected, and private. There are four access levels: public, protected, default, and private. Classes can have only public or default access.
What is the difference between default and public class access ?
A class with default access can be seen only by classes within the same package. A class with public access can be seen by all classes from all packages.
What nonaccess modifiers can have a class ?
Classes can also be modified with final, abstract, or strictfp.
is the following class definition correct and why:
public final abstract class Tortoise { }
A class cannot be both final and abstract. By definition, an abstract class is one that must be extended by another class to be instantiated, whereas a final class can’t be extended by another class. By marking an abstract class as final, you’re saying the class can never be instantiated, so the compiler refuses to process the code.
Can an abstract class be instantiated ?
When a class is required to be marked as an abstract one?
What types of method can have an abstract class ?
When the abstract methods in an abstract class should be implemented ?
An abstract class cannot be instantiated.
A single abstract method in a class means the whole class must be abstract.
An abstract class can have both abstract and nonabstract methods.
The first concrete class to extend an abstract class must implement all of its abstract methods.
What are the differences and similarities between interfaces and abstract classes ? Can an abstract class implement an interface ?
An interface is like a 100% abstract class and is implicitly abstract whether you type the abstract modifier in the declaration or not.
An interface can have only abstract methods, no concrete methods allowed.
Interface methods are by default public and abstract—explicit declaration of these modifiers is optional.
A class implementing an interface can itself be abstract. An abstract implementing class does not have to implement the interface methods (but the first concrete subclass must).
What is called class member ?
Methods and instance (nonlocal) variables are known as “members.”
What access modifiers can class members have ?
Members can use all four access levels: public, protected, default, and private.
How class visibility is related to its members visibility ?
If a class cannot be accessed, its members cannot be accessed. Determine class visibility before determining member visibility.
Difference between public and private member access modifier ?
public members can be accessed by all other classes, even in other packages. If a superclass member is public, the subclass inherits it—regardless of package. 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.
What “this.” means ?
this. always refers to the currently executing object. this.aMethod() is the same as just invoking aMethod().
Difference between default and protected member access modifier ?
Default and protected members differ only when subclasses are involved:
- Default members can be accessed only by classes in the same package;
- Protected members can be accessed by other classes in the same package, plus subclasses regardless of package;
- protected = package + kids (kids meaning subclasses);
- For subclasses outside the package, the protected member can be accessed only through inheritance; a subclass outside the package cannot access a protected member by using a reference to a superclass instance. (In other words, inheritance is the only mechanism for a subclass outside the package to access a protected member of its superclass.);
- A protected member inherited by a subclass from another package is not accessible to any other class in the subclass package, except for the subclass' own subclasses;
Can local variables have non-access or access modifiers ?
Local (method, automatic, or stack) variable declarations cannot have access
modifiers. final is the only modifier available to local variables.
Do local variables get default values ?
Local variables don’t get default values, so they must be initialized before use.
What other non access member modifiers have ?
- final methods cannot be overridden in a subclass;
- abstract methods are declared with a signature, a return type, and an optional throws clause, but they are not implemented. The first nonabstract (concrete) class to extend an abstract class must implement all of the abstract class’ abstract methods. abstract methods end in a semicolon—no curly braces and they could not be final or private;
- the synchronized modifier applies only to methods and code blocks. synchronized methods can have any access control and can also be marked final;
- The native modifier applies only to methods - indicate that the method is implemented in native code using JNI(Java Native Interface);
- The strictfp modifier applies only to classes and methods - ensures that you get exactly the same results from your floating point calculations on every platform;
What is var-arg method ?
Var-arg method is methods that can declare a parameter that accepts from zero to many arguments. A var-arg parameter is declared with the syntax type… name; for instance: doStuff(int… x) { }. A var-arg method can have only one var-arg parameter. In methods with normal parameters and a var-arg, the var-arg must come last.
Can we declare a local and instance variable with the same name ?
It is legal to declare a local variable with the same name as an instance
variable; this is called “shadowing.”