Flag This Hub

Java Jornal: Access Modifiers

By


In this article, we will look into how it mean to use an access modifier in a method or a variable declarations inside a class. Methods, fields, constructors and nested classes declared within the body of a class are known as members. The following bellow are all four types of access modifiers in java:

  • public
  • protected
  • default
  • private

All those four access modifiers can be used for members unlike class who we know we can only use public and default. However, our focus on this article is how to deal with all four access modifiers on members. I think its pretty obvious that variable and methods are both different and distinct. For now, I would like you to ignore their differences.

Assuming that we have a class named A who can access a member from class named B. Therefore, class B is visible from class A. Another class named C is trying to access a member in class B which technically class B does not allow. In this case, the compiler will slap class C with an exception for trying to access a member in class B that class C should supposedly not know it exist. What I'm trying to point out is that for us to determine if a method code within the body of a class is able to access a member of another class. Also, to determine if a subclass can inherit a member of its superclass.

There is two ways to access a member from another class. It is either through a dot(.) operator or through inheritance. See below for the examples.

class Greeting {
	public String sayHello(){
		return "Hello!"
	}
}

class DotOperator {
	public void greetPerson{
		Greeting greeting = new Greeting();
		System.out.println("Dot operator: " + greeting.sayHello());
	}
}

class UsingInheritance extends Greeting{
	public void greetPerson{
		System.out.println(sayHello());	
	}
}

You can see from the example above which illustrates how to access a method from another class through a dot operator and by inheritance. However, don't forget that classes have specific access control. Keep in mind that if class A is not accessed by class B then, it's automatically assumed that members of class A will not be access by class B at all. It does not make sense to look over access control of its member when your not sure that the class itself is granted an access to another class.

Public Members

If a certain method or variable member is declared public, this means that all classes no matter what package they belong to, will be able to access the member. This is assuming that the class is visible.

Protected Members

If a certain method or variable member is declared protected, this means that the variable is accessible to all other class within thesame package or by inheritance if class is located on other package wants access to a protected member. However, if class B, who inherit class A's protected member, is inherit by class C, an attempt to access the protected member from class A will render a compiler error. This is because when a subclass inherits the protected member, it automatically becomes a private member of the subclass.

Default Members

If a certain method or a variable member is declared default, this means that it is accessible within the package level only. Please remember that if you don't place an access modifier on a method, class, or a variable, it is automatically recognized by the compiler as default. Default method can be access within the class where it was declared, invoke using a reference to a class the method was declared, or by invoking method inherited from superclass.

Private Members

If a certain method or variable member is declared private, this means that the member can only be access in the class which the private member was declared. All other class beside the owner class of the private member will not be able to access the member. However, if is neccessary to change the value of the private member, it is a good practice to use public accessor methods so that code in any other class has to ask to set or get a variable rather than accessing the variable directly. Also, it is best to make all variables private and just use public accessor methods to access the variable.

Summary Table

Visibility
Public
Protected
Default
Private
From the same class
Yes
Yes
Yes
Yes
From any class in the same package
Yes
Yes
Yes
No
From a subclass in the package
Yes
Yes
Yes
No
From a subclass outside the same package
Yes
Yes, through inheritance
No
No
From any no-subclass class outside the package
Yes
No
No
No

Moreover, access modifiers are essentially very important in java. It is likely your controller on which methods are made available to everyone, restricted to some or unavailable to everyone.

I hope this helps. For more information about access modifiers in java, do visit java.sun.com.

Comments

nicomp 4 months ago

Good stuff. Right more.

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working