View RSS Feed

Hibernate

Packages, accessibility/visibility, synthetic access and protected constructors

Rate this Entry
by , 08-12-2011 at 07:21 PM (5419 Views)
In Java it is common to use packages, especially for libraries.
Two common packages, that are built in to Java, are java.io and java.util.
(Another, even more common is java.lang, but that one is imported automatically.)

Before you can use a package [without using the reflection classes] you most import it in
the file (not class) that uses it. To import the package java.io, you add the line
Java Code:
import java.io.*;
just below your package line (if any; otherwise in the top of the).
But if you only want to use java.io.File you add
Java Code:
import java.io.File;
instead.

There is however another possible, although ugly (except with classes have the same name), way to
using a class from another package: fully qualified class names.
If you do not import the class, or package, you most write, for example:
Java Code:
java.io.File file = new java.io.File("/");
Instead of:
Java Code:
import java.io.File;
//[⋅⋅⋅]
File file = new File("/");
Further reading: Static Import


Assume your Web site's address is www.example.org, and your want to create a program called Game.
It is then conventional to call your package (which may contain packages of its own) org.example.Game.
All files in that package most have as its first non-comment/-empty line:
Java Code:
package org.example.game;
Your classpath is the folder where you put all your files and folders, in it you must now have
the folder org/example/game, where all files in that package is located.
The package file is located in is automatically imported, so you do not write:
Java Code:
package org.example.game;
import org.example.game.*;
All packages are accessible form all packages, with the example of the default package.
The default package is the nameless package located directly in your classpath, it is the
package used when you do not specify package. The default package is not accessible from other packages.

Further reading: Packages


Accessibility (or visibility) of member is the property of from where a member is accessible/visible.
An accessibility modifier is a (or none) keyword you add to a member (a class, interface, enum, method or variable).
There are there such keywords: public, protected and private.
The is also an accessibility used when not such keyword is used, the default accessibility or package private.
public is the most visible, followed by protected, package private (default) and private.

Public members are accessible from everywhere.
Private members are only accessible from the same class/enum.
Package private members are only accessible from the same package.
Protect members are only accessible from the same package as well as from instances (non-static members) of subclasses.

There is however a synthetic sugar called synthetic access. It also you to write code where
a class's inner class access a private members of the outer class. This is used implicitly and
you may get a warning from your compiler.


Finally, there is an [synthetic salt] exception for the protected access modifier.
Constructors with the access modifier protected cannot be access from subclass,
but it can be used in the first line in the constructor, to specify which constructor should
used.

Java Code:
package superclass;
public class Superclass
{
    protected Superclass()
    {
        //[⋅⋅⋅]
    }
}

-------------------------------------

package subclass;
import superclass.*;
public class Subclass extends Superclass
{
    public Subclass()
    {
        super(); //Allowed "use" of superclass's protected constructor.
        Superclass superclass = new Superclass(); //Non-allowed use of the superclass's protected constructor.
    }
}

Submit "Packages, accessibility/visibility, synthetic access and protected constructors" to Facebook Submit "Packages, accessibility/visibility, synthetic access and protected constructors" to Digg Submit "Packages, accessibility/visibility, synthetic access and protected constructors" to del.icio.us Submit "Packages, accessibility/visibility, synthetic access and protected constructors" to StumbleUpon Submit "Packages, accessibility/visibility, synthetic access and protected constructors" to Google

Tags: None Add / Edit Tags
Categories
Java's syntax

Comments