Declaring methods Using Access Modifiers in Java

Hi friends, Here we will discuss about Access Modifiers. Every Programmer must and should know the basics of programming language then only we can write the code efficiently. Java access modifiers help structure  of our program to have proper scoping. The access modifiers specify the access control of methods and variable members of a class. There are the four levels of access controls:

  • public
  • private
  • protected 
  • default

We can use the default or public access modifiers in the case of classes, but all the four access modifiers can also be used for class members. Here, we will focus on the following two different access issues:

  •  Whether or not the method declared in a class can access members of the other class.
  • whether or not a subclass can inherit a member of its superclass.

Public Method

Public is the most well known of the Java keywords. Public is also the easiest of the Java access modifiers because of its nature. All classes,irrespective of the package they belong to, can access all public class members of a class. For example, let’s we declare the Book class in the com.vbbs.book package and also define the getBook() method as public.

Here we Declaring the Book Class

  1. package com.vbbs.book;
  2. public class Book
  3. {
  4. //declaring public method as class member
  5. public void getBook() {
  6. system.out.println(“The getBook() method is accessesed”);
  7. }
  8. }

The getBook() method to be accessed by other classes,as it declared public. Now, let’s create the Order class in the com.vbbs.order package to access the public members of the Book class.

Here the order class.

  1. package com.vbbs.order;
  2. package com.vbbs.book;
  3. public class Order
  4. {
  5.   public static void main(String[] args)
  6.   //Creating an instance of the Book Class
  7.     Book b = new Book();
  8.    b.getBook();
  9. }
  10. }

The Order class is defined in the com.vbbs.order package and invokes the getBook() method as it is declared public. Therefore, the public class members defined in a class can be accessed by any other class using the dot(.) operator.

Irrespective of the package, a subclass inherits the public members of a superclass. For example we create a superclass MySuper and the subclass MySub extending the MySuper superclass.

The MySuper class

  1. package com.vbbs.book;
  2. public class MySuper
  3. {
  4. public String display()
  5. {
  6. return “hello”;
  7. }
  8. }

The display()  method is declared public and returns a string hello. Now let’s create the Mysub class, which extends the Mysuper class and calls the display() method of its superclass.

The MySub class

  1. package com.vbbs.order;
  2. import com.vbbs.book.*;
  3. public class MySub extends Mysuper
  4. {
  5.   public void disp()
  6. {
  7.    system.out.println(display());
  8. }
  9.  public static void main(String[] args)
  10. {
  11.  MySub sub = new Mysub();
  12. sub.disp();
  13. }
  14. }

The display() method is invoked without a reference or a dot(.) operator as it is implicitly accessesed using the this reference. The this reference always refers to the currently executing object. We can also access the display() method in other classes by  creating  an instance of the Mysub class.

  1. …….
  2. MySub sub = new MySub();
  3. system.out.println(sub.display());

The sub instance of the MySub class is created,which extends the MySuper Class.

Private Method

The private class members can only be accessed by the class in which they are declared.In the example of the Book and Order classes, let’s declare the getBook() method as private.

The Book Class Defining a Private Class Member

  1. package com.vbbs.book;
  2. public class Book
  3. {
  4.  private void getBook(0 {
  5.  system.out.println(“The getBook() method is accessed”);
  6. }
  7. }

here declares the getBook() method private and, so it cannot be accessed by any other class.If we tries to access the getBook() method from any other class, then a compilation error occurs:

  1. package com.vbbs.order;
  2. import com.vbbs.book.*;
  3. public class Order
  4. {
  5.   public static void main(String[] args) {
  6. //Creating an instance of the book class
  7. Book b = new Book();
  8. //Accessing private member of the Book class
  9. b.getBook();
  10. }
  11. }

on compiling the above code displays the compilation error, when a private member is accessed outside the class in which it is declared:

A private class member can only be accessed from within the class in which it is declared and not from outside the class in which it is declared. Now, let’s discuss whether a subclass  can inherit the private number of its subclass.Now, we declare the display() method as private,and check whether or not the MySub accesses the private display() method.

  1. ……
  2. public class MySuper
  3. {
  4. private String display()
  5. {
  6. return “hello”;
  7. }
  8. }

On compiling the above code, a compilation error occurs indicating that the display() method is not a member of the MySub class.This implies that the subclass cannot inherit the private class members of its superclass.

Protected and default Methods

The default class member can be accessed when the declaring and accessing class belongs to the same package.

The protected class members are accessed by a subclass, even if it belongs to a different  package.

For Example,let’s mark the default access for the getBook() methodof the Book class.

The Book class Defining the default Class member

  1. package com.vbbs.book;
  2. public class Book
  3. {
  4.   //declaring default method as class member
  5. void getBook()
  6. {
  7. System.out.println(“The getBook() method is accessed”);
  8. }
  9. }

Now, when we access the getBook() method from another class, that is, the Order class then a compilation error occurs. This is due to the fact that the default member can be accessed by any class within the package.Here, the Book class belongs to the com.vbbs.book package and the Order class belongs to the com.vbbs.order package.

Now, in the case of protected members, the subclass can access the protected members of its superclass. Let’s consider the MySuper and MySub classes and define the display() method as protected.

  1. …………..
  2. public class Mysuper {
  3. protected String display() {
  4. return “hello”;
  5. }
  6. }

Now, the MySub class extending the MySuper class can inherit the display() method and access it. Here we should be noted that the default access class members can be accessed by a class within the same package. Morever, the protected access class members can be accessed by a subclass belonging to either the same package or any other package.

 

Leave a Reply

Your email address will not be published. Required fields are marked *