There are 4 access modifiers that are used in Java programming language
• public
• protected
• default
• private
default protection is what you get when you don’t provide an access modifier in the member declaration.
Section 1.01 Public Members
When a method or variable is declared public, it means all other classes, regardless of the package they belong to, can access the member.
Example: Take a class MyClass
public class MyClass
{
public void show()
{
System.out.println(“show in MyClass”);
}
}
Now call show() method in other class, Mytest
public class MyTest
{
public static void main(String[] args)
{
MyClass obj = new MyClass();
obj.show();
}
}
Compile both the programs and run MyTest
It should give the output:
E:\Java>java MyTest
show in MyClass
Section 1.02 Private Members
Members marked private cannot be accessed by code in any class other than the class in which the private member is declared
Example: Taking class MyClass
public class MyClass
{
private void show()
{
System.out.println(“show in MyClass”);
}
}
Now, try to call show() in MyTest class
public class MyTest
{
public static void main(String[] args)
{
MyClass obj = new MyClass();
obj.show();
}
}
When we will compile the MyTest.java, it will give an error:
E:\Java>javac mytest.java
mytest.java:6: show() has private access in MyClass
obj.show();
^
1 error
Section 1.03 Protected Members
A protected member can be accessed by the classes within the same package or (through inheritance) by a subclass even if the subclass is in different package.
Example 1: (On how to access a protected member from the same package)
Taking a class MyClass in package mypkg;
package mypkg;
public class MyClass
{
protected void show()
{
System.out.println("show in MyClass");
}
}
Now trying to access show() method from MyTest class from the same package, mypkg
package mypkg;
import mypkg.MyClass;
public class MyTest
{
public static void main(String[] args)
{
MyClass obj = new MyClass();
obj.show();
}
}
The code will compile and run successfully.
Example 2: (On how to access a protected member from different package via inheritance)
Taking a class MyClass in package mypkg;
package mypkg;
public class MyClass
{
protected void show()
{
System.out.println("show in MyClass");
}
}
Now trying to access show() method from MyTest class in mytest package
package mytest;
import mypkg.MyClass;
public class MyTest extends MyClass
{
public static void main(String[] args)
{
MyTest obj = new MyTest();
obj.show();
}
}
The code will compile and run successfully.
Section 1.04 Default Members
A default member may be accessed only if the class accessing it belongs to the same package.
Example: Taking class MyClass in package mypkg:
package mypkg;
public class MyClass
{
void show() //default member
{
System.out.println("show in MyClass");
}
}
Now calling it in MyTest class which belongs to the same package:
package mypkg;
import mypkg.MyClass;
public class MyTest
{
public static void main(String[] args)
{
MyClass obj = new MyClass();
obj.show();
}
}
Compile the code and run MyTest. It will run the show method successfully!!!
Note: Thanks to macosxnerd101 for reminding me a point in protected member explanation. For any questions you have in this topic, do let me know, i'll try to figure it out.
Regards,
King_009





MultiQuote



|