Access specifiers:
Private :TBA
Protected: TBA
Public : TBA
Access Modifiers:
- There are three access modifiers: public, protected and private.
- There are four access levels: public, protected, default and private.
- Classes can have public or default accesses.
- 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.
- Class visibility revolves around whatever code in one class can
Class Modifiers (Non Access):
- Classes can also modified with final, abstract or strictfp
- A class cannot be final and abstract
- A final class cannot be sub classed
- 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 non abstract methods.
- The first concrete class extends an abstract class must implement all of its abstract class methods.
Interface declaration:
- Interfaces are constants 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 from any inheritance tree.
- An interface is like a 100 percent abstract class and it is implicitly abstract weather you type the abstract modifier in the declaration or not.
- An interface is can have only methods, no concrete methods allowed.
- Interface methods are by default public and abstract explicit deliration of these modifiers is optional.
- Interface can have constants, which are always implicitly public, static and final.
- Interface constants declarations of public, static and final are optional in any combination.
- A legal non abstract implementing class has the following properties:
- It provides concrete implementations for the interface methods.
- It must follow all legal override rules for the methods it implements.
- It must not declare any new checked exceptions for implementation methods.
- It must not declare any checked exceptions that are broader then the exceptions declared in the interface method.
- It may declare runtime exceptions on any interface method implementation regardless of the interface declaration.
- If it must maintain the exact signature (allowing for covariant returns) and return type of the methods it implements (but doesn’t nave to declare the exceptions of the interface).
- A class implementing the interface can itself be abstract.
- An abstract implementing class doesn’t have to implement the interface methods (but the first concrete class must).
- A class can extend only one class (no multiple interface), but it can implement many interfaces.
- Interface can extend one or more interfaces.
- Interface cannot extend a class or implement a class or interface.
Member Access Modifiers:
- Methods and instance (nonlocal) variables are known as “members”.
- Members can use all four access levels: public, protected, default and private.
- Member access comes in two forms
- Code in one class can access a member of another class.
- A subclass can access a member of another class.
- If a class cannot be accessed its member cannot be accessed.
- Determine class visibility before determining the member visibility.
- Public members can be accessed by all the other classes even in the other packages.
- Member accessed without dot (.) operator must belong to the same class.
- this Always refers to the current executing object.
- aMethod() is same as just invoking aMethod();
- Private member can only access by the code in the same class.
- Private members are not visible to the sub classes, so private members cannot be inherited.
- Default and protected differ only when subclasses are invoked.
- Default members can ne accessed only by classes in the same package, and sub classes regarding to that package.
- Protected members can be accessed by other classes in the same package and subclasses regardless of package.
- Protected = package plus kids (kids means 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 super class instance (in other words, Inheritance is the only mechanism for a subclass outside the package to access a protected member of its super class.
- 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 package.
Local Variables:
- Local (method, automatic or stack) variables declarations cannot have access modifiers.
- Final is the only modifier available for local variables.
- Local variables don’t get default values so they must be initialized before use.
Other modifiers- Members:
- Final methods cannot be overridden in the subclasses.
- Abstract methods are declared, with a signature, a return type, and an optional throws clause but are not implemented.
- Abstract methods ends with a semicolon not with curly braces.
- Three ways to spot a non-abstract method
- The method is not marked abstract.
- The method has curly braces.
- The method has code between the curly braces
- The first non-abstract (concrete) class to extend an abstract class must implement all of the abstract classes’ abstract methods.
- The synchronized modifier applies only to methods and code blocks.
- Synchronized methods can have any access control and can also be marked final.
- Abstract methods must be implemented by a subclass, so they must be inheritable, fro that reason:
- Abstract methods cannot be private.
- Abstract methods cannot be final.
- The native modifier applies to apply only to methods.
- Strictfp modifier applies only for class and methods.
Methods with var-args:
- As on java 5, methods can declare a parameter that accepts from zero to many arguments, a so-called var-args method.
- 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.
Variable declarations:
- Instance variables can:
- Have any access control.
- Be marked final or transient.
- Instance variables can’t be abstract, synchronized, native or strictfp.
- It is legal to declare a local variable with the same name as an instance variable; this is called “shadowing”.
- Final variables can have the following properties:
- Final variables cannot be reinitialized once assigned a value.
- Final reference variables cannot be refer to a different object once the object has been assigned to the final variable.
- Final reference variables must be initialized before the before the constructor completes.
- There is no such thing as final object. An object reference marked final does not mean the object itself is immutable.
- The transient modifier applies only to instance variables.
- The volatile modifier applies only to instance variables.
Array Declarations:
- Arrays can hold primitives and objects, but the array itself is always an object.
- When you declare an array the brackets can be to the left or right of the variable names.
- It is never legal to include the size of an array in the declaration.
- An array of objects can hold any object that passes the IS-A (of instance of) test for the declared type of the array. For example, if Horse extends Animal, then a Horse object can go into an Animal array.
Static Variables and Methods:
- Static variables or methods are not tied to any particular instance or class.
- No class’s instances are needed in order to use static members of the class.
- There is only one copy of a static variable / class and all instances share it.
- Static methods do not have direct access to non-static members.
Enums:
- An enum specifies a list of constant variables assigned to at type.
- An enum is NOT a String or int, an enum constant’s type is the enum type. For example. SUMMER and FALL are of the enum type Season.
- An enum can be declared outside a class must NOT be marked static, final, abstract, protected or private.
- Enums can contain constructors, methods, variables and constant class bodies.
- Enum constant send arguments to the enum constructor, using the syntax BIG (8), where the int literal 8 is passed to the enum constructor.
- Enum constructors can have arguments, and can be overloaded.
- Enum constructors can NEVER be invoked directly in code. They are always called automatically when an enum is initialized.
- The semicolon at the end of an enum declaration is optional. These are legal:
enum foo{ ONE, TWO, Three}
enum foo{ ONE, TWO, Three};
- values() returns an array if MyEnum’s values.