X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Modifier types in Java :

Whenever we are writing our own Java class it is compulsory that we have to provide some information about our class to the JVM like whether our class can be accessible from anywhere or not, whether child class creation is possible for our class or not, whether instantiation is possible or not etc.

We can specify these information by declaring with appropriate modifier. The only applicable modifiers for top-level classes are :

  • public




  • <default>




  • final




  • abstract




  • strictfp




If we are using any other modifier then we will get compile time error saying modifier xxx not allowed here.

Example :

private class Example { public static void main(String [ ] args) { System.out.println("Algo"); } }

Output:

Compile time error : modifier private not allowed here

But for inner classes the following modifiers are allowed :

  • public




  • <default>




  • final




  • abstract




  • strictfp




  • private




  • protected




  • static




In old languages like C and C++ public, private, protected are considered as access specifiers and final, static etc. are considered as access modifiers. But in Java there is no such division and all are considered as access modifiers.


Access Control and Inheritance :

The following rules for inherited methods are enforced :

  • Methods declared public in a superclass also must be public in all subclasses.

  • Methods declared protected in a superclass must either be protected or public in subclasses but they cannot be private.

  • Methods declared without access control (no modifier was used) can be declared private in subclasses.

  • Methods declared private are not inherited at all, so there is no rule for them.

Non-Access Modifiers :-

Java provides a number of non-access modifiers to achieve many other functionality :

  • The static modifier for creating class methods and variables.

  • The final modifier for finalizing the implementations of classes, methods, and variables.

  • The abstract modifier for creating abstract classes and methods.

  • The synchronized and volatile modifiers, which are used for threads.

To use a modifier, we include its keyword in the definition of a class, method, or variable. The modifier precedes the rest of the statement, as in the following example :

Example :

public class Example { // ... } private boolean b; static final double d = 10.5; protected static final int i = 10; public static void main(String [ ] args) { // body of method }


Member (variable and method) :


public member :

If we declare a member as public then we can access that member from anywhere but corresponding class should be visible(public) i.e. before checking member visibility we have to check class visibility.

If a class declared as public then we can access that class from anywhere.

Example :

package p1; public class Algo { public void m1() { System.out.println("Algo"); } } package p2; import p1.Algo; public class Example { public static void main(String [ ] args) { Algo a = new Algo(); a.m1(); } }

Output:

Algo

If we are not declaring class Algo as public then we will get compile time error while compiling class Example saying p1.Algo is not public in p1 can't be accessed from outside package.


protected member :

If a member is declared as protected then we can access that member anywhere within the current package but outside package it can be accessible only in child classes.

Within the current package we can access protected member either by parent reference or by child reference.

But from outside package we can access protected member only by using child reference. If we are trying to use parent reference then we will get compile time error.

package p1;
public class Algo
{
 protected void m1()
 {
   System.out.println("Algo");
 }
}
public class Example1 extends Algo
{
 public static void main(String [ ] args)
 {
   Algo a = new Algo();
   a.m1(); // valid
   Example1 e1 = new Example1();
   e1.m1(); // valid
   Algo a1 = new Example1();
   a1.m1(); // valid
 }
}

package p2;
import p1.Algo;
public class Example2 extends Algo
{
 public static void main(String [ ] args)
 {
   Algo a = new Algo();
   a.m1(); // invalid
   Example2 e2 = new Example2();
   e2.m1(); // valid
   Algo a1 = new Example2();
   a1.m1(); // invalid
 }
}

default member :

If a member declared as default then we can access that member only within that current package i.e. we can't access it from outside of the package. Hence, default access is also known as package level access.

If a class declared as default then we can access that class only within that current package i.e. we can't access it from outside of the package.


private member :

If a member is declared as private then we can access that member only within the current class. An abstract method should be visible in child classes to provide implementation where as private method is not visible in child classes. Hence, private - abstract combination is illegal for method.



X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.