Question :

Explain various Access Modifiers in Java.

Subject

Java

Standard

Class 10

Views

1449

Asked By

William

Advika
Answer / Solution

There are two types of modifiers in Java, these are Access modifiers and non-access modifiers.

The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and class by applying the access modifier on it.

There are four types of Java access modifiers:

  • Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
  • Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.
  • Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.
  • Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.
Vedhika
Answer / Solution

Java Access Modifiers

A Java access modifier specifies which classes can access a given class and its fields, constructors and methods. Access modifiers can be specified separately for a class, its constructors, fields and methods. Java access modifiers are also sometimes referred to in daily speech as Java access specifiers, but the correct name is Java access modifiers. Classes, fields, constructors and methods can have one of four different Java access modifiers:

  • private
  • default (package)
  • protected
  • public

Each of these Java access modifiers will be covered in the following sections of this Java access modifier tutorial. The following table summarizes what Java constructs each Java access modifier can be applied to:

Private Access Modifier

If a method or variable is marked as& private(has the code>private access modifier assigned to it), then only code inside the same class can access the variable, or call the method. Code inside subclasses cannot access the variable or method, nor can code from any external class.

There is an example of assigning the private access modifier to a field:

public class Clock {
    private long time = 0;
}

Accessing private Fields via Accessor Methods

In some cases the fields are truly private, meaning they are only used internally in the class. In other cases the fields can be accessed via accessor methods. In the above example the two methods getTime() and setTime() can access the time member variable.

public class Clock {
    private long time = 0;
    public long getTime() {
        return this.time;
    }
    public void setTime(long theTime) {
       this.time = theTime;
    }
}

Private Constructors

If a constructor in a class is assigned the private Java access modifier, that means that the constructor cannot be called from anywhere outside the class. Here is a Java class example of private constructor.

public class Clock {
    private long time = 0;
    private Clock(long time) {
        this.time = time;
    }
    public Clock(long time, long timeOffset) {
        this(time);
        this.time += timeOffset;
    }
    public static Clock newClock() {
        return new Clock(System.currentTimeMillis());
    }
}

This version of the Clock class contains a private constructor and a public constructor. The private constructor is called from the public constructor (the statement this();). The private constructor is also called from the static method newClock().

Default (package) Access Modifier

The default Java access modifier is declared by not writing any access modifier at all. The default access modifier means that code inside the class itself as well as code inside classes in the same package as this class. Therefore, the default access modifier is also sometimes referred to as the package access modifier. Here is an default / package access modifier example. In given example the subclass SmartClock has a method called getTimeInSeconds() which accesses the time variable of the superclass Clock

public class Clock {
    long time = 0;
}
public class ClockReader {
    Clock clock = new Clock();
    public long readClock{
        return clock.time;
    }
}

Protected Access Modifier

The protected access modifier provides the same access as the default access modifier, with the addition that subclasses can access protected methods and member variables (fields) of the superclass.

Here is a protected access modifier example:

public class Clock {
    protected long time = 0;    // time in milliseconds
}
public class SmartClock() extends Clock{
    public long getTimeInSeconds() {
        return this.time / 1000;
    }
}

public Access Modifier

The Java access modifier public means that all code can access the class, field, constructor or method, regardless of where the accessing code is located. The accessing code can be in a different class and different package. Here is a public access modifier example:

public class Clock {
    public long time = 0;
}
public class ClockReader {
    Clock clock = new Clock();
    public long readClock{
        return clock.time;
    }
}

Class Access Modifiers

The Java access modifiers private and protected cannot be assigned to a class. Only to constructors, methods and fields inside classes. Classes can only have the default (package) and public access modifier assigned to them. If the class is marked with the default access modifier, then no other class outside the same Java package can access that class, including its constructors, fields and methods. It doesn't help that you declare these fields public, or even public static. It is important to keep in mind that the Java access modifier assigned to a Java class takes precedence over any access modifiers assigned to fields, constructors and methods of that class.

Interface Access Modifiers

Java interfaces are meant to specify fields and methods that are publicly available in classes that implement the interfaces. Therefore you cannot use the private and protected access modifiers in interfaces. Fields and methods in interfaces are implicitly declared public if you leave out an access modifier, so you cannot use the default access modifier either (no access modifier).

Recent Question Update

Advantages Of NCERT, CBSE & State Boards Solutions For All Subjects

  • All the NCERT Solutions have been prepared by academic experts having 10+ years of teaching experience.
  • They have prepared all the solutions in simple and easy language so that each and every student can understand the concepts easily.
  • All the solutions have been explained step to step-wise in details with better explanations.
  • Students can also use these question and answers for your assignments and in homework help.
  • All the solutions have been explained in detail and the answers have been compiled in a step-wise manner.
  • All the questions and answers are commonly prepared according to the Latest Syllabus of Board Education and Guidelines.
  • Students can know about the various types of questions asked in the exams with the help of these solutions.

Top Course Categories