OCJP 6
Questions to the Certification Oracle Java 6 Programmer based on the study guide
Questions to the Certification Oracle Java 6 Programmer based on the study guide
Kartei Details
Karten | 51 |
---|---|
Sprache | English |
Kategorie | Informatik |
Stufe | Universität |
Erstellt / Aktualisiert | 08.05.2013 / 18.04.2016 |
Weblink |
https://card2brain.ch/box/ocjp_6
|
Einbinden |
<iframe src="https://card2brain.ch/box/ocjp_6/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>
|
Class
A template that describes the kinds of state and behavior that objects of its type support
Object
At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its own state, and access to all of
the behaviors defined by its class.
State (instance variables)
Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object's instance variables make up the object's state.
Behavior (methods)
When a programmer creates a class, she creates methods for that class. Methods are where the class' logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.
What does it mean to access a class? When we say code from one class (class A) has access to another class (class B), it means class A can do one of three things:
- Create an instance of class B.
- Extend class B (in other words, become a subclass of class B)
- Access certain methods and variables within class B, depending on the access control of those methods and variables
-> in effect, access means visibility.
Access to Class Members:
public
- From the same class
- from any class in the same package
- from a subclass in the same package
- from a scubclass outside the same package
- from any non-subclass class outside the package
Access to Class Members:
protected
- from the same class
- from any class in the same package
- from a subclass in the same package
- from a subclass outside the same package (THROUGH INHERITANCE)
Access to Class Members:
default
- from the same class
- from any class in the same package
- from a subclass in the same package
Access to Class Members:
private
- from the same class
byte
- 8 bits
- 1 byte
- mim. Range: -27
- max. Range: 27-1
short
- 16 bits
- 2 byte
- mim. Range: -215
- max. Range: 215-1
int
- 32 bits
- 4 byte
- mim. Range: -231
- max. Range: 231-1
long
- 64 bits
- 8 byte
- mim. Range: -263
- max. Range: 263 - 1
float
- 32 bits
- 4 bytes
double
- 64 bits
- 8 bytes
modifiers on local variables
final
modifiers on non-local variables
- final
- public
- protected
- private
- static
- transient
- volatile
modifiers on methods
- final
- public
- protected
- private
- static
- abstract
- synchronized
- strictfp
- native
Effect of final:
Final class
final class cannot be subclassed
Effect of final:
final method
final method cannot be overridden by a subclass
Effect of final:
Final variable
- final variable cannot be assigned a new value, once the initial method is made
- final reference variables cannot refer to a different object once the object has been assinged to the final variable
- final reference variables must be initialized before the constructor completes
Identifier rules:
- 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
- javaBeans methods must be named using camelCase, and dependign on the method's purpose, must start with set, get, is, add or remove
Declaration Rules:
- a source code file can have only one public class
- if the source file contains a public class, the filename must match the public class name
- a file can have only one package statement, but multiple imports
- the package statements (if any) must be the fist (non-comment) line in a source file
- the import statements (if any) must come after the package and before the class declaration
- if there is noc package statement, import statements must be the first (non-comment) statements in the source file
- package and import statements apply to all classes in the file
- a file can have more than one nonpublic class
- files with no public classes have no naming restrictions
Access modifiers
- public
- protected
- private
Access levels
- public
- protected
- private
- default
Possible access modifiers for classes
- public
- default
Nonaccess class modifiers
- a class cannot be both final and abstract
- a final class cannot be subclassed
- 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
Interface Implementation
- interfaces are contracts for what a class can do, but they say nothing about the way in which the class must do it
- interfaces can be implemented by any class, form any inhertance tree
- an interface is like a 100% abstract class, and is implicitly abstract whether you type the abstract modifier in the declaration or not
Interface methods
- an interface can have only abstract methods, no concrete method ist allowed
- interface methods are by default public and abstract - explicit declaration of these modifiers is optional
Constants in interfaces
- interfaces can have constants, which are always implicitly public, static and final
- interface constant declarations of public, static and final are optional in any combination
a legal nonabstract implementation class has the following properties
- 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 runtime exceptions on any interface method impementation regardless of the interface declaration
- it must maintain the exact signature (allowing for covaraint returns) and return type of the methods it implements (but does not have to declare the exceptions of the interface)
a class implementing an interface can itself be abstract
true
How many classes can a class extend, how many interfaces implement?
a class can extend only one class (no multiple inheritance), but it can implement many interfaces
How man interfaces can an interface extend
Interfaces can extend one or more other interfaces
members
- methods
- nonlocal variables
access modifiers on members
- public
- protected
- private
- default
member acces comes in two forms
- code in one class can acsess a member of another clas
- a subclass can inherit a member of its superclass
member access rules
- if a superclass memeber is public, the subclass inherits it - regardless of package
- members accessed without the dot operator (.) must belong to the same class
- this. always refers to the currently executing object
- this.aMehtod() is the same as just invoking aMethod()
- private members can be accessed only by cone in the same class
- private members are not visible to subclasses, so private members cannot be inherited
default and protected members
- differ only when subclasses ar 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 reagdless of package
- protected = package plus kids (subclasses)
- 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 form another package is not accessible to any other class in the subclass package, except for the subclass own subclasses
Local variables
- loca (method, automatic, or stack) variable declarations cannot have access modifiers
- final is the only modifiers available to local variables
- local variables don't get default values, so they must be initialized before use