Java Access Control 5 Min Drill

  • The scoping is defined as life of a variable ( or a reference variable ).
  • The scope of an automatic variable is method scope and the member variable life is class level.
  • Identifiers can begin with a letter, an underscore, or a currency character.
  • After the first character, identifiers can also include digits.
  • main() method can be overloaded
  • Java access modifiers are public, private, protected.
  • Java access levels are 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.
  • Classes can also be modified with final, abstract, or strictfp
  • Class cannot be both final and abstract
  • final class cannot be subclassed
  • 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. Otherwise that class should be abstract class and so on
  • A 'child' class cannot access private features of 'parent' class in the inheritance hierarchy.
  • Protected features are available to class, subclass, within a package and only for subclasses in a different package.
  • If you don't specify an access modifier, then compiler assigns default access modifier, which is not a keyword.
  • The static feature does not have "this" reference.
  • A 'final' variable must be initialized at the point of declaration.
  • final is the only modifier available to local variables.
  • final methods cannot be overridden in a subclass.
  • final variables cannot be reassigned once assigned a value.
  • final reference variables cannot refer to a different object once the object has been assigned to the final variable.
  • final variables must be initialized before the constructor completes.
  • abstract methods are declared with a signature, a return type, and an optional throws clause, but they are not implemented (upto Java 1.7).
  • Local variables don't get default values, so they must be initialized before use.
  • If define a class as final then the class cannot be extended (avoiding the inheritance).
  • Methods, instance, nonlocal variables are known as "members."
  • Local (method, automatic, or stack) variable declarations cannot have access modifiers.
  • If a class cannot be accessed, its members cannot be accessed.
  • public members can be accessed by all other classes, even in other packages.
  • If a superclass member is public, the subclass inherits it—regardless of package.
  • private members can be accessed only by code in the same class.
  • private members cannot be inherited.
  • Default members can be accessed only by classes in the same package.
  • protected = package + 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.
  • The synchronized modifier applies only to methods and code blocks.
  • synchronized methods can have any access control and can also be marked final.
  • A var-arg parameter is declared with the syntax type... e.g. doRefresh(String... java) { }.
  • Instance variables can marked final or transient
  • shadowing => declare a local variable with the same name as an instance variable
  • Static Variables and Methods are not tied to any particular instance of a class.
  • No class instances are needed in order to use static members of the class.
  • Only one copy of a static variable/class and all instances share it.
  • static methods do not have direct access to nonstatic members.
  • The native modifier applies only to methods.
  • The strictfp modifier applies only to classes and methods
  • transient, volatile modifier applies only to instance variables.