Declaration and Access Control

Posted on Posted in Java

Identifiers

  • identifiers begin with letter, underscore or currency
  • After the first letters, identifiers can contain digits
  • can be any length
  • must be camelCase methods
    • Example of all valid identifier
String b, _hello, $holder, number5, 
reallyreallyreallyreallylongName;

Declaration Rules

  • source code file can only have one public class
  • file name must match class name
  • one package statement, with multiple imports
    • package must be first line
    • import must come after
  • file can have more than one non public class

Class Access Modifiers

  • four access levels
    • public
    • protected
    • private
    • default
  • Classes: public and default
  • Class visibility revolves around whether another class can:
    • Create ans instance of another class
    • extend another class
    • access methods and variables of another class

Non Access Class Modifiers

  • non access modifiers: final, abstract, or strictfp
  • class cannot be both final and abstract – contradicting
  • final class cannot be subclassed
  • abstract cannot be instantiated
  • single abstract method means the whole class must be abstract
  • abstract class can have both concrete and abstract class
  • first concrete class to extend abstract must implement all of its abstract methods

Interface

  • Interface are contracts for what a class can do
  • interfaces are implemented by class
  • interface can only have abstract methods
  • interface methods are default public and default
  • interface can have constants but they are implicity public static and final
  • A legal nonabstract implementing 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 implementation regardless of the interface declaration.
    • It must maintain the exact signature (allowing for covariant returns) and return type of the methods it implements (but does not have to declare the exceptions of the interface).
    • Interfaces cannot extend a class, or implement a class or interface
    • Interfaces can extend one or more other interfaces
    • A class can extend only one class (no multiple inheritance), but it can implement many interfaces.

 

 

Leave a Reply