Page 1 of 1

Access Modifiers For Class Members Explanation with examples of 4 access modifiers used in Java language Rate Topic: -----

#1 king_009  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 37
  • Joined: 20-August 09

Posted 01 September 2010 - 11:44 PM

Access Modifiers in Java
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


Is This A Good Question/Topic? 3
  • +

Replies To: Access Modifiers For Class Members

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 278
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Posted 03 September 2010 - 02:43 PM

Very good tutorial. It seems like a lot of people have trouble grasping the concepts of modifiers and how to use them in practice but this is an excellent tutorial that will definitely help out a a lot of people.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1