23 February 2008

Declarations and Access Control (Part II)

Identifiers :

  • 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 depending 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 statement (if any) must be the first (non-comment) line in a source file.
  • The import statements (if any) must come after the package and before the class declaration.
  • 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.
Class Access Modifiers :
  • 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.
  • 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 whether code in one class can create an instance of another class, extend (or subclass), another class and access methods and variables of another class.
Class Modifiers :
  • Classes can also be modified with final, abstract, or strictfp.
  • 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.
  • 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.
  • An interface can have only public and abstract methods, no concrete methods allowed.
  • Interfaces can have constants, which are always implicitly public, static, and final.
  • Interfaces can only extend one or more other interfaces.
Member Access Modifiers
  • Members can use all four access levels: public, protected, default, private.
  • public members can be accessed by all other classes, even in other packages.
  • private members can be accessed only by code in the same class.
  • 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.
  • 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.

1 comment:

Anonymous said...

A source code file can have only one public class.

can you tell me why this rule is created ? any specific reasons