Witscale Test Center

Chapter summary


Chapter summary

This chapter covers major part of the first SCJP objective, specifically sections 1.1, 1.2 and 1.3. In this chapter we overviewed how modifiers affect the accessibility and behavior of classes, methods and variables. Here is a summary of the important concepts learned in this chapter.

 

       Access modifiers

         public

         Least restrictive of all access modifiers

         Applicable to top-level classes, nested classes, methods and member variables

         protected

         Applicable to methods, member variables and nested classes. The top-level class cannot be declared as protected.

         Allows access to all subclasses as per inheritance

         Also allows access to all the classes in the same package

         (default)

         Absence of any modifier indicates a default or package access.

         Applicable to top-level classes, inner classes, methods and member variables

         Allows access to all the classes in the same package

         private

         Most restrictive of all access modifiers

         Applicable to inner classes, methods and member variables

         Allows access only to the declaring class

q       The non-access  modifiers

         abstract

         Applicable to classes and methods

         Abstract class cannot be instantiated

         If a class has one or more abstract methods, it must be declared as abstract

         Interfaces are abstract by default

         All methods in Java interface are also abstract by default

         final

         Applicable to classes, methods and variables

         A final variable is constant and cannot be changed once initialized

         A final object-reference variable cannot be changed. However the object it is referring to can be.

         Final methods cannot be overridden.

         A final class cannot be sub classed. All methods of a final class are final by default

         All variables declared in an interface are final by default.

         static

         Applicable to methods, member variables, free-floating blocks and inner classes

         A static variable is associated with a class; hence it can be changed by referring to either the class or its instances. Similarly a static method can be invoked either by referring the class or its instances.

         A static method cannot access non-static members of class.

         Static initializer block is executed only once at the time of class loading.

         native

         Applicable only to methods.

         Native methods are only declared in Java class, their implementation lies in native library.

         The native library containing native method’s implementation must be loaded before that native method is invoked.

         A native method cannot be declared as abstract.

         synchronized

         Applicable to methods and code blocks.

         Ensures that only one thread can execute that method or code block at a time.

         transient

         Applicable only to the member variables.

         Transient variables of an object are not serialized when that object is serialized.

         Interface constants cannot be transient.

         volatile

         Applicable only to member variables.

         Used in multithreaded environment to ensure that the variable’s modifications are synchronized across all the threads.

       Constructors

         Constructor and access modifiers

    Access modifiers of class’s constructors decide which classes can create an instance of that class.

    When all constructors of a class are made private, no other class can create instances. Such class should provide mechanism to create and access its instance.

         Declaration restrictions for constructors

    Constructor is not inherited from the superclass.

    Only access modifiers are applicable to constructors. They cannot be declared as static, final or abstract. 

          The default constructor

    When class definition does not have any constructor, the compiler creates a default constructor with no parameters. Its body simply has a call to the superclass’s constructor-with-no-arguments.

    The default constructor is given the same access modifier as the class.