Class Declaration and Access Modifiers

0

A class is declared using the keyword class, giving the class a name and listing the class members between curly braces.

class A {
}

Class Access Modifiers

A class declaration can be preceded by class modifiers that give class certain character. These modifiers are categorized into access and non-access modifiers.

We need a way to control who has access to what class or interface, and what members of the type. This control is specified with access modifiers.

In this article, we will look into examples of access modifiers on class.  Access modifiers restrict or allow access to a class you create. All members of a class are always available to code within the class.

To control access from other classes, we have four access modifiers:

  • public
    A class declaration with public keyword gives all classes from all packages access to the public class. If the class accessing the public class is in a different package, you will need to import the public class.
public class A {
}
  • packaged (default access)
    This is the access control you get when you don’t specify a modifier in the class declaration. This is the default access that one gets when modifier is not specified. A class with package-level access can be seen only by classes within the same package.
class A {
}
  • protected and private
    A class can be declared with only public or default access. Only nested classes can be protected or private.

Class Declaration Rules

Now I will show you some examples which will cover the class access modifiers in detail.
There can be only one public class per source code file.

package javarticles.somepackage;
public class C {
}
public class OtherC {    
}

The public type OtherC must be defined in its own file

The error clearly indicates that the public class must be in its own file. You must also make sure the file name is same as the name of the public class.
A file can have more than one non-public class.
You cannot have more than one public class but you can have one or more packaged scope class declared after the public class. For example:

package javarticles.publicaccess;
public class E {
}
class EFPackaged {    
}

You can also have a protected, package or a private scoped class declared but they have to be enclosed within the public class.
For example:

package javarticles.publicaccess;

public class E {
    protected class EEProtected {
        public EEProtected(){}        
    }
    class EEPackaged {        
    }    
    private class EFPrivate {        
    }
}
class EFPackaged {    
}

How to access packaged scope and protected scope classes?

First, we will see an example of a class which is in the same package. In the below example, Class G is in the same package as class E declared above. Since it is in the same package it will be able to access the outer package class EFPackaged, inner package class EEPackaged and the protected class EEProtected.

package javarticles.publicaccess;

public class G {
    public void doSomething() {
        //outer package class
        EFPackaged f = new EFPackaged();
        
        //inner package class
        E e = new E();
        E.EEPackaged ee1 = e.new EEPackaged();
        
        //protected class
        E.EEProtected ee2 = e.new EEProtected();
    }
}

Now we will see an example of a class J which is in a different package. It will only be able to access the protected scope class EEProtected but for that it has to extend E. If the class is in a different package it can access the protected class through inheritance but it won’t be able to access private and packaged scope classes.

package javarticles.somepackage;

import javarticles.publicaccess.E;

public class J extends E {
    public void doSomething() {
        E e = new E();
        E.EEProtected ee = e.new EEProtected();
    }
}

File with only non-public classes
File with no public classes can have a name that does not match any of the classes declared in the file. For example, file K has below package scope class M and L:

package javarticles.somepackage;

class M {
 protected class B {
 }
}
class L {
}

If I remove the package javarticles.somepackage, compiler will throw error:
The declared package “” does not match the expected package “javarticles.somepackage”.
If I move the package declaration next to import, I get a different error:

import javarticles.publicaccess.D;
package javarticles.somepackage;
public class C extends D {
}

Syntax error on token “package”, import expected

Thus if the class is part of a package, the package statement must be the first line source code file.

You cannot have a protected class as the main class, a class can be declared with only public or default access.

package javarticles.protectedaccess;

protected class BProtectedScope {
}

Illegal modifier for the class BProtectedScope; only public, abstract & final are permitted

You can declare a protected class but it has to be within the public or package class.

package javarticles.publicaccess;

public class E {
    protected class EEProtected {
        public EEProtected(){}        
    }    
    private class EFPrivate {        
    }
}

Create a class in package scope and try accessing it from some other package.

package javarticles.packageaccess;

class APackageScope {
}

Now try creating an instance of APackageScope from a class declared in a different package.

package javarticles.publicaccess;

import javarticles.packageaccess.APackageScope;

public class D {
    public void createInstanceOfA() {
        APackageScope a = new APackageScope();
    }
}

Compiler throws errors:
The type javarticles.packageaccess.APackageScope is not visible
APackageScope cannot be resolved to a type

You can’t access a packaged scoped class from any other package other than the package where it is declared.

In my next article, I will show you examples of members access modifiers.

Download the code here.

Share.

Leave A Reply