Access Specifiers, Attributes and Methods in JAVA

Access Specifiers, Attributes and Methods in JAVA

In this part of the tutorial you will learn. 1) What are the Access Specifiers in JAVA and how we use them? 2) What are the attributes in JAVA and how we use them? 3) What are the Methods in JAVA and their use? What are the Access Specifiers in JAVA and their use? The access to the contents of the class’s attribute and the methods is determined by the access specifiers. There are three kinds of access specifiers • Public • Protected • Private By using the public keyword against the name of class, attribute or method we declare it as public and they can be used by all the classes within the same package but in case we need to use the contents of the class in some other package the class which had been made public has to be imported. Example: For making something public we use ‘public’ key word before that class, attribute or the method.
  1. Public void main(String[] arguments)
  2. {
  3. //some code
  4. }
The concept of the protected access specifier is related to the super class and the subclass. The subclass is the one which is derived from the super class. By using the protected access specifier in the super class the methods, attributes etc can be accessed in the same way that they are public. Protected access specifier only allow the related class to use each other functions. Example: In this example the protected method the super class is being used in the subclass.
  1. class myclass{
  2.    protected void mymethod(variable v) {
  3.       //the operation to be performed is written here
  4.    }
  5. }
  6.  
  7. class submethod{
  8. void mymethod(variable v) {
  9.       //the operation to be performed is written here
  10. }
  11. }
Private access specifiers are used when we want to use the methods within the scope of the same class. It is the most restricted access specifier. None of the private access specifier’s object can be called outside the class hence it ensures a lot of security.
  1. Class myclass
  2. {
  3.                Private void mymethod(variable v)
  4.                 {
  5.                   //body of the method
  6.                           }
  7. }
What are the attributes in JAVA and how we use them? Attribute is the member variable of a class. Every class is made up of methods and attributes. The attributes can also be said as the properties or the features of the class. Suppose we are humans, and we have our name, our height etc. This height and name are said to be the attributes of the class human. Example:
  1. public class human
  2. {
  3. private String name="John";
  4. private int height=180; //in cm
  5. }
What are the Methods in JAVA and their use? Group of instructions combined together to perform a certain operation are termed as methods in JAVA. In C programming the Methods are referred to as functions. They can also be interchangeably used for both languages. Methods always have a return type and input argument types associated with it. Example:
  1. Public void add(int a,int b)
  2. {
  3. System.out.printin(a+b);
  4. }
This method takes in two variables ‘a’ and ‘b’ , after addition prints out the result to screen using ‘System.out.println’. As the return type is void so we do not return any value, rather we print it.

Add new comment