Java- class level modifiers / Chapter -1/ Access Modifier


java Access Modifier

Access Modifier



Whenever we are writing our own classes we have to provide some information about our class to JVM like:-



  1. whether this class can be accessible from anywhere or not?

  2. Whether child class creation is possible or not?

  3. Whether the object creation is possible or not?


We can answer these questions by using an appropriate modifier.

Class Level Modifiers

 The only applicable modifiers for Top-Level Classes are:-

public 
<default>
final
abstract
strictfp


But for inner-classes the applicable modifiers are:-


public 
<default>                        
final                    +           private 
 abstract                            protected  
strictfp                              static


example:-

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

Compile -Time error:  modifier private not allowed here.

Let's understand more about these modifiers one by one.


1.public modifier for classes:-
If a class declared as public then we can access that class from anywhere.
example:-

package pack1;
public class A
{
public void m1()
{
System.out.println("Hiii");
       }
}

javac -d . A.java

this command will compile the java program and placed the .class file into the specified directory or in the specified folder.

package pack2;
import pack1.A;

public class B
{
public static void main(String args[])
{
A a =new A();
a.m1();
}
}
javac -d . B.java     compiling
java pack2.b            running

Output is: Hiii

Note: - If a class is not declared as public then while compiling B class, we will get compile time error saying:-
pack1.A is not public in pack1: can not be accessed from outside package.



2.default modifier for classes:-




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

Hence default classes are also known as package level classes.

 

example:

package pack1;
class A
{
public void m1()
{
System.out.println("Hiii");
       }
}javac -d . A.javathis command will compile the java program and placed the .class file into the specified directory or in the specified folder.

package pack2;
import pack1.A;

public class B
{
public static void main(String args[])
{
A a =new A();
a.m1();
}
}
javac -d . B.java     compiling
Compile Time error: cannot access from default classes.

Next:- Java final Modifier/Chapter -2/ Access Modifier
Home:



No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com