Chapter 1 - Declarations and Access Control Flashcards
To pass the SCJP 6 exam
Access Modifiers Members can use
public
protected
default
private
Access Modifiers Classes can use
default
public
Class
A template that describes the kinds of state and behavior that objects of this type support
Object
When JVM encounters new keyword, make an instance of that class. Object will have its own state, and access to all behaviors defined by its class.
State
Each object has its own unique set of instance variables as defined by the class. All instance variables make up the objects state
Inheritance
Code in one class can be reused in other class. Superclasses are extended to more specific sub classes. Subclass inherits accessible instance variables and methods.
Interface
100-percent abstract superclass that identified the methods a subclass must support, but not how they must be supported.
Legal identifiers
- start with a letter, $, or connecting character like a _. Identifiers CANNOT start with a number
- after 1st character, can contain letters, $, connecting characters or numbers
- no limit to # of characters
- cant use JAVA keywords
- Identifiers are CASE sensitive
Classes and interfaces
First letter should be Capitalized, if more than 1 word, use camelcase.
Methods
First letter should be lowercase, then normal camelCase
Variables
start with lowercase, use camelCase
CONSTANTS
UPPER_CASE. use underscores for more than 1 word.
JavaBeans
Java classes that have properties. Properties are private instance variables only accessible by getter and setter methods
JavaBeans Naming Rules #1
If property is NOT boolean, methods prefix must be get, example getSize()
JavaBeans Naming Rules #2
If property is boolean, the getter method is get or is. For example getStopped() or isStopped()
JavaBeans Naming Rules #3
Setter method must prefex set, example setSize()
JavaBeans Naming Rules #3
Setter methods must be marked public, with void return type
JavaBeans Naming Rules #4
Getter methods must be marked public, take no arguments, and have a return type that matches the argument type of setter method for that property
JavaBean Listener Naming Rules #1
Listener methods names used to register a listener, must use the prefix add, followed by listener type: addActionListener()
JavaBean Listener Naming Rules #2
Listener methods used to remove a listener must use the prefix remove
JavaBean Listener Naming Rules #3
Listener method names must end with the word “Listener”
Source File Declaration Rules #1
There can be only one public class per source code file
Source File Declaration Rules #2
Comments can appear at the beginning or end of any line in the source code
Source File Declaration Rules #3
If there is a public class in a file, the name of the file must match the name of the public class. Example: public class Dog { } must be in source file as Dog.java
Source File Declaration Rules #3
If class is part of package, the package statement must be the first line in source file, before any import statements
Source File Declaration Rules #3
If there are import statements, they must go between the package statement and the class declaration. If NO package statement, import statements MUST be first line of source
Source File Declaration Rules #4
import and package statements apply to all classes within a source code file.
Source File Declaration Rules #5
A file can have more than one nonpublic class
Source File Declaration Rules #6
Files with no public classes can have a name that does not match any of the classes in the file.
Modifiers fall into two categories
Access modifiers: public, protected, private
Non-access modifiers: strictfp, final, and abstract
Class Access - 3 things
When code has access to another class:
- Create an instance of class
- Extend class
- Access certain accessable methods and variables
Default Access
Class has no modifier preceding it in the declaration. Think of it as package-level access. Classes with default can only be seen by classes within same package.
Public Access
A class declared as public gives all classes from all packages access to the public class.If public class you are accessing is in different package, must import the public class.
Can a class be marked as final and abstract
NO
Final Class
final class means the class can’t be subclassed. Guarantees none of the methods in the class will ever be overridden
Abstract class
can NEVER be instantiated. Methods in abstract class end in semicolon rather than curly pair brace {}. If a single method is marked as abstract, the WHOLE class must be declared as abstract
Interface
declares what a class can do, without saying anything about HOW the class will do it.
Interface
implicitly public and abstract
must NOT be marked as static