Basics Flashcards
When is a prefix operator evaluated?
Before its FIRST use in the expression
When is a postfix operate evaluated?
After it is FIRST used in the expression
Which packages are automatically inherited into every Java source file?
java.lang. This does not mean that you can’t import it explicitly, its just not required.
How many public classes can appear at the top level of a source file?
Only 1. There can be public nested classes, but not more than 1 top-level public class. You can have more than 1 class at the top level so long as only 1 is public
Does an abstract class need abstract methods to be abstract?
No. An abstract class simply means it is not a concrete class and cannot be instantiated.
Can you import a class statically?
No, only static methods can be imported. “import static a.b.C” where C is a class is not permitted
Can you import a method statically?
Yes. To import all static methods of a class use the .* notation or just reference the method name to import only that method
Does importing a class give you direct access to its members?
No, you must explicitely import them.
Is it valid to use () in front of a method name when importing that static method
No, this is not allowed.
What is the order for declaration of class/interface definitions, import statements and package statements?
(1) Package statement (2) import statements (3) class/interface declarations
How many package statements can occur in a source file?
1 and only 1. If there is no package statement then the classes/interfaces defined in the class can NEVER be referenced by a class in another package
What java statements can be annotated with the abstract keyword and what does it mean in each case?
(1) abstract class - it cannot be instantiated (2) abstract method - needs to be implemented by child classes and the class must be declared abstract. You CANNOT have abstract variables.
Where can the final keyword appear and what does it mean?
(1) final class - you cannot extend the class (2) final variable - you cannot change the variable once its value is set either in the declaraiton or the constructor (3) final method - you cannot override the method
Where can the keyword static appear and what does it mean?
(1) nested classes NOT top-level classes (2) variables (3) methods - in each case, only 1 entity appears per class in the JVM
Define what class cohesiveness means?
It is ensuring that each of your classes only does what it is intended for with clear boundaries and responsibilities
Descrbe each of the rules used by the Java compiler to determine if an identifier is legal?
(1) The identifier can start with loweror upper case alphabet, (2) can’t start with numbers, (3) can start with underscore or connecting symbols, (4) can start with any currency-based symbol. (5) Can be any length in theory. (6) cannot be a java keyword, and is case-sensitive (7) the characters used can be any letter, currency characters, connecting characters and numbers
Are interfaces objects?
No.
What are the 8 rules of source file declarations?
(1) There can only be 1 public class in a source code file (2) Comments can appear anywhere
(3) Package statements, if included must be the first line of the file
(4) If there is a public class, it must match the name of the source code file
(5) If there are import statements they must appear between the package statement and class/interface declarations
(6) import and package statementa apply to all class in the file - you cannot declare classes in the same source code file in different packages or with different import statements
(7) A file can have more than one nonpublic class
(8) Files with no public class do not have to have a class whose name matches the source code file
What are the 5 requirements for a method to be executable in a class?
(1) call main
(2) return type is void
(3) accepts an array of strings
(4) be static
(5) be public
Note this means the main method can be final
Can the main() method be overloaded?
Yes, but any other main method will not be executable. It will just be treated as a normal main method.
Can a main method accept a varargs?
Yes. The following is perfectly legitimate -
public static void main (String .. args)
What is a classes fully qualified name? Give an example
The fully qualified name of a class is its class name plus the package path to get to it e.g. java.util.ArrayList and not just ArrayList.
Can you use a static import statement to import non-static entities?
No - you can only use static imports to import static members of a class
What is the order of the static and import keywords in a static import statement?
always, always “import static”. You cannot have static import instead
Describe import statements
Import statements allow you to save typing by importing classes that are not part of this classes package. In order to use a class you must explicitly import it or it is included as part of a wildcard.
e.g. import java.util.*; or import java.util.ArrayList will both give you access to ArrayList. You can also import all static methods of a class by doing static import java.lang.System.*;
Bear in mind that if you are importing two classes with the same name, the compiler will complain and you will have to explcitely use their fully qualified names or rename the classes
What will java.*; do?
This will import all non-static classes and interfaces in the java package if there are any. It will compile even if the package contains no classes or interfaces.
What are the two other names typically associated with inner classes?
Nested classes and anonymous classes
What is a top-level class?
It is a class that appears at the top level of a Java source code file
What are the two types of modifiers and what modifiers fall into each type?
Access and non-access. Access modifiers - public, protected, private; Non-access - final, static, abstract
What are the two access control modifiers that can be used on a top-level class?
public and none. (package private).
Why can’t you have a protected top-level class in a source code file?
This is semantically equivalent to using the public access modifier. It makes sense that the class is only visible in the package but not for sub-classing, as it would need to be made public to be visible.
Can you combine final and abstract? Why?
Final prevents the class from being subclasses, but asbtract means the class has to be subclassed because it cannot be instantiated, hence they cannot be used together.
What Java entities can strictfp be used on?
A class and method, but never a variable. OCP to explain why
Why would you use final on a class?
Final means that you cannot subclass that class and is only used when you want to guarentee the implementation of that class where none of your methods are overriden in the entire class. If it is not the entire class, then use final on the method which is more common. Ask - If this class is subclassed, then could this change the expected behaviour in a bad way? Use it carefully, as you are effectively saying that this class cannot be improved upon or substituted with a subclass if a problem is found with it. Using final stops your code from being extensible which is bad OO design.
What is an abstract class and why would you use it?
Abstract classes cannot be instantiated. They don’t require abstract methods. They exist soley to be subclassed and provide a way to create general/abstract behaviour to all subclasses.
NOTE : You can compile and even execute an abstract class - you just can’t instantiate it.
Can abstract apply to methods and variables?
Only classes and methods. It doesn’t make sense to have an abstract variable. If a class has abstract methods, then it must also be declared as abstract.
When marking a method as abstract what else must you do?
Not provide a method body defined by the curly brackets. You do, however, provide any parameters for the signature including () if that is necessary.
Are interfaces abstract?
Yes, but it is redundant to mark an interface as abstract as it is implied in the nature of the entity. All interfaces are abstract! Likewise, you don’t have to mark a method as being abstract in an interface, even though an interface contains only abstract entities (methods).
What is an interface?
An interface is a contract that can be implemented by any class. It is a 100% abstract class.
NOTE : The compiler adds “public abstract” to all method declarations in an interface and the class is implicitely public.
What are the 9 rules of an interface?
(1) All interface methods are implictely public and abstract
(2) Any variables declared must be public, static and final
(3) Interface methods cannot be static
(4) Methods cannot be marked as final because they are inheritently abstract
(5) An interface can extend one or more other interfaces
(6) An interface is not an object and cannot extend an object
(7) An interface cannot implement another interface
(8) Declared with the keyword interface rather than class
(9) Interface types can be used polymorphically
Can you explicitely declare an interface as abstract?
Yes, but it is redundant.