position: fixed; top: auto !important; margin-left: 112px;

Classes & Access Modifiers

What is Java Class ? Why is it being used in Object Oriented programming language? What is actually an Object Oriented programming?

Do you play games? Yes, a game like Super Mario, Pacman, breakout game, snake game, etc. These different types of games are the part of one big blueprint named as "GAME" :). So there is something which creates a blueprint of various types of games and keeps them together in a player like video games device (eg, Play Station etc).

What is Class ?

As I explained in above example , Class is just a blueprint which contains stuff like properties , variables, member functions and all . A class defination starts with class keyword followed by classname and class body is enclosed by pair of paranthesis

Syntax :-

modifier class
{

}

Before diving deep into Access Modifiers in Java , make sure you setup your project as I explained in Installation tutorial. Please refer before starting .

Access Modifiers

There are various types of Access Modifiers in Java as follows :-

1) public

2) private

3) protected

1) public access:-

When a method or variable member is declared public, it means all other classes, regardless of the package they belong to, can access the member (assuming the class itself is visible).

Example :-

Assume there are two different packages (Packages in Java are nothing but folders in simple language) named aPackage & bPackage. aPackage contains one class & bPackage contains one class.

package aPackage;
import bPackage.*;      // Import all classes in the bPackage package
class SuperClass
{
  public static void main(String[] args)
  {
  SubClass sub = new SubClass();
  sub.display();
  } 
}

Now look at the second file:

package bPackage;
public class SubClass
{
  public void display()
  {
    System.out.println("This is subclass"); 
  }
}

Output :-

This is subclass

Here, there are two packages (Consider Packages as folders always) named as aPackage & bPackage . aPackage contains SuperClass.java & bPackage contains SubClass.java Both the classes have public access.

In first file Superclass.java we created an instance of Subclass and called its display() method and since that display method was public inside Subclass that's why method is visible in Superclass. And as you see both the files were in two different folders (Packages) public access allows to use its methods from different classes too .

2) private access :-

When a method or variable member is declared private, no other class , no other packages can access the private properties except the class in which those private members or methods are written. This access modifier is called as private access modifier.

Example :-

Assume there are two different packages (Packages in Java are nothing but folders in simple language) named aPackage & bPackage. aPackage contains one class & bPackage contains one class.

package aPackage;
import bPackage.*;      // Import all classes in the bPackage package
class SuperClass
{
  public static void main(String[] args)
  {
  SubClass sub = new SubClass();
  sub.display();        // Compilation error at this line 
  } 
}

Now look at the second file:

package bPackage;
public class SubClass
{
  private void display()    
  {
    System.out.println("This is subclass"); 
  }
}

As you see display() method in SubClass is private . So in SuperClass when it tries to access display method, compilation error appears because you cannot access private methods. But SubClass is public thats why you can access it but not its methods . If you make SubClass modifier also private , then

package aPackage;
import bPackage.*;      // Import all classes in the bPackage package
class SuperClass
{
  public static void main(String[] args)
  {
  SubClass sub = new SubClass();   // Compilation error at this line 
  sub.display();        // Compilation error at this line 
  } 
}

Now look at the second file:

package bPackage;
private class SubClass
{
  private void display()    
  {
    System.out.println("This is subclass"); 
  }
}

3) default vs protected Access

default access allows to access the members & methods only if both classes are in same folder (package) whereas protected access allows to access the members & methods of same class, subclass through inheriatnce irrespective of any package access. Any package classes can use methods but only through inheritance.


Let's discuss about default access mofifier in same package and in different package. It works in same package through inheritance but not in another package.

default access in same package

package aPackage;
  class SuperClass {
    void display() {   // No modifier means method has default
                       // access 
      System.out.println("First Class Method");
    }
}
  
 public class SubClass extends SuperClass
  {
    public static void main(String[] args)
    {
   SubClass sub = new SubClass(); 
   sub.display();
    }
  }

Output :-

First Class Method

As you see, display method in SuperClass contains no access modifier. So access modifier in this case is default. default modifier will not allow to use methods of superclass object of another package but in same package it will works fine through inheritance( As you see inside SubClass, we are calling the display() method of SuperClass through Subclass instance sub)

deafult Access in another package :-

package aPackage;
  public class SuperClass {
    void display() {   // No modifier means method has default
                       // access 
      System.out.println("First Class Method");
    }
}
package bPackage;
import aPackage.*;
class SubClass
{
  static public void main(String[] args)
     {
      SuperClass firstClass = new SuperClass();
      firstClass.display(); // Compilation Error
     } 
}

As you see, the display method in SuperClass contains no access modifier. So access modifier in this case is default. default modifier will not allow using methods of superclass object of another package but in the same package, it will work fine through inheritance. But in this program, both the classes are in different packages. SuperClass is in aPackage and SubClass is in bPackage and if we tried to call display() method of SuperClass in SubClass then compilation error is being shown.

protected access :-

protected access is used to access the methods of same class plus another class in same as well as in another package but only through inheritance.

package aPackage;

public class SuperClass {
   protected void display()
    {
      System.out.println("SuperClass Method");
    }
}
package bPackage;
import aPackage.*;
public class SubClass extends SuperClass{
     public static void main(String []args)
     {
       SubClass superClass = new SubClass();
       superClass.display();
     }
}

Output :-

SuperClass Method.

Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.