Access Specifiers and Modifiers
Classes enable an object to access data variables or methods of another class. Java provides access specifiers and modifiers to decide which part of the class, such as data members and methods will be accessible to other classes or objects and how the data members are used in other classes and objects.
An access specifier controls the access of class members and variable by other objects. The variouse types of access specifiers in Java are:
- The public access specifier. A member of a class declared public can be accessed anywhere in the same class, package in which the class is created. you can use a public class, data member, or method from any object in a Java program. Note that a package is a collection of classes. A class’s public members are accessible wherever the program has a reference to an object of that class. It’s advisable that methods be declared public while instance variables be declared private. An example of declaring variables and methods with the public access specifier:
public string name = “Ginowine”;
public void setName ()
The access specifier public in the first example shows that the variable name is accessible wherever a program has a reference to an object of the class in which the variable is declared. Classes in the same package with the class of the name variable declared public can also access it.
- private access specifier. This specifier provides most restricted level access. A data member of a class declared private is accessible at the class level only in which it is declared. It is a fact that in Java, you implement the concept of encapsulation by suing the private keyword. Below is example of declaring a variable private
private int age = 30;
and this is how to declare a method private:
private int getAge ()
The private specifier in the first example show’s that variable age can only be access from inside the class in which it was created. Same goes for the method example, it can only be accessed from within the class it was created. They are not directly accessible outside the class of their declaration.
- Protected access specifier. Variables and methods declared protected are accessible only to the subclass of the class in which they are declared. It is sometimes said that protected access specifier offers an intermediate level of access between public and private specifiers. A superclass’s protected members can be accessed by members of that superclass, by members of its subclass and members of other classes in the same package. Subclass methods can refer to public and protected members inherited from the superclass simply by using the member name. when a subclass method overrides a superclass method, the superclass method can be accessed from the subclass by preceding the method superclass method name with keyword super and dot (.) separator. An example of declaring a member as protected.
Protected float salaray = 350 ;
- Friendly or package Access Specifier. If you do not specify any access specifier, the scope of data members and methods is friendly. Java provides a large number of classes, which are organized into groups in a package. A class variable or method that is declared friendly is accessible only to the classes of a package in which they are declared. Example of declaring a member protected include:
Int age = 30 ;
Remember that the default access specifier if you do not specify one directly is protected, so the above statement has no access specified as such it is a friendly variable and can only be accessed by classes in the same package.
Types of Modifiers
Modifiers determine or define how the data members and methods are used in other classes and objects. The main difference between access specifiers and modifiers is that access specifiers define the accessibility of the data members in a class and the modifiers determine how these methods are used and modified by other classes. The various modifiers permitted in Java include:
Every object has its own copy of all the instance variables of the class. In certain cases, only one copy of a particular variable should be shared by all objects of a class. A static field is used in such cases. A static variable represents classwide information, all objects of the class share the same piece of data. The declaration of a static variable begins with the keyword static.
The final keyword is used with methods, variables, and classes. The final modifier indicates that the data member cannot be modified and any attempt to modify it is an error. The final modifier does not allow the class to be inherited. It is used to create classes that serve as a standard and you do not want anybody to modify the methods in a subclass and use them in a different way. A class can be declared final if you do not want the class to be subclassed.
The abstract keyword is used to declare classes that only define common properties and behaviour of other classes. An abstract class is used as base class to derive specific classes of the same type.
The native modifier is used only with methods. It is used to inform the compiler that the method has been coded in a programming language other than Java, such as C++ or C. the native keyword with a method indicates that the method lies outside the Java Runtime Environment (JRE).