Forum Stats

  • 3,727,350 Users
  • 2,245,373 Discussions
  • 7,852,750 Comments

Discussions

Object class

Arpit1992
Arpit1992 Member Posts: 10
edited May 2015 in Java 8 Questions

Why finalize() and clone() is protected in java and not public ?

Arpit1992

Answers

  • Unknown
    edited May 2015
    Arpit1992 wrote:
    
    Why finalize() and clone() is protected in java and not public ?
     

    Because the general rule for using access qualifiers is to use the most restrictive qualifier that satisifies the need for access.

    See the various sections of The Java Language Spec

    4.3.2 The Class Object
    The class Object is a superclass (§8.1.4) of all other classes. . . . The finalize method declared in class Object takes no action. The fact that class
    Object declares a finalize method means that the finalize method for any class
    can always invoke the finalize method for its superclass. This should always
    be done, unless it is the programmer's intent to nullify the actions of the finalizer
    in the superclass. (Unlike constructors, finalizers do not automatically invoke the
    finalizer for the superclass; such an invocation must be coded explicitly.)
    . . . 6.6.2 Details on protected Access A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object. . . .

    4.3.2 The Class Object
    The class Object is a superclass (§8.1.4) of all other classes.

    The finalize method declared in class Object takes no action. The fact that class
    Object declares a finalize method means that the finalize method for any class
    can always invoke the finalize method for its superclass. This should always
    be done, unless it is the programmer's intent to nullify the actions of the finalizer
    in the superclass. (Unlike constructors, finalizers do not automatically invoke the
    finalizer for the superclass; such an invocation must be coded explicitly.)


    6.6.2 Details on protected Access
    A protected member or constructor of an object may be accessed from outside
    the package in which it is declared only by code that is responsible for the
    implementation of that object.
    . ..

    8.4.3. Method Modifiers
    MethodModifier:
    (one of)
    Annotation public protected private
    abstract static final synchronized native strictfp

    . . .
    13.4.7 Access to Members and Constructors Changing the declared access of a member or constructor to permit less access may break compatibility with pre-existing binaries, causing a linkage error to be thrown when these binaries are resolved. Less access is permitted if the access modifier is changed from package access to private access; from protected access to package or private access; or from public access to protected, package, or private access. Changing a member or constructor to permit less access is therefore not recommended for widely distributed classes.

    So all of the above rsult in:

    1. The access modifiers are: package, private, protected and public

    2. The Object class contains the methods 'clone' and 'finalize'

    3. All classes inherit/extend the Object class so inherit the methods 'clone' and 'finalize'

    4. All classes therefore can execute those methods (although the base methods do nothing)

    5. There can be issues with narrowing the access of a class or method

    In sum - there is NO REASON to make the methods public since they are already accessible to any class that you create as those classes MUST inherit from the Object class.

    Arpit1992
Sign In or Register to comment.