Request a Call back


captcha

Java Training – Learn How to Define the Scope of Elements in Java Programs with Access Modifiers

Access modifiers are the most important elements defining the scope of variables, methods, classes and constructors used in the program. These are a part of Java basics and can be well learnt in a through Core Java training course.

Access modifiers are the important elements of a java program that define the scope of various variables, classes, methods and constructors in the program. They define the structure of a program and hence their clean and effective use is important for efficient programming. Every core java learner would be aware of the usage of these access specification keywords because each and every Core Java training course usually starts with training on the basic programming fundamentals which also include the access modifiers too. Hence, taking up a Core Java training course in a reputed institute can help you gain better understanding of the basic concepts for further advancing into the vast Java programming world! Through such Java course, an individual can learn the ways to define the scope in Java and hence, learn the strategies of defining proper structure for the programs.

There are essentially three different types of access modifiers used in Java programming explicitly – public, private and protected, while the one called “default” is set automatically to the elements where no access modifier has been specified.

Let us now see in detail about each and every access specifier in Java with an appropriate example.

Public:

A variable or method starting with the keyword “public” would be accessible all round the program. Usually the variables and functions that are to be accessed from everywhere in the program, are set to public. Public is least protective of the access controllers and hence, need to be used wisely to allow only those entities which you want to be accessed by anything or everything in the program.

There would be four access levels basically in a java program with respect to the access of variables, methods, classes. They are the class, package, sub class and other packages. The elements of the program defined as public would be accessed across all these levels freely.

For example:

package training;

public class JavaTraining

{

public void java()

{

public int demo=0;

System.out.println(“This is a Java Course”);

}

Now in the above example you can access the variable demo or the method java() anywhere in the entire program without any restriction. For instance, if you want to access java() in another package called training1, then all you need to do is to import training package into training1 with following code.

package training1;

import training.java();

Private:

This is the most protective of the access modifiers in Java and hence, does restrict the scope of the variables, classes or methods within the same class. The elements declared as private can nowhere else be accessed other than that class itself.

Consider the below example:

class JavaTraining {

private int numberOfstuds=10;//private variable

public void showDemo() {

System.out.println(“The Variable value is ” + numberOfstuds);

}

private void testDemo() {

System.out.println(“It cannot be accessed in another class”);

}

}

public class JavaTraining1

{

public static void main(String[] args) {

JavaTraining jt = new JavaTraining();

jt.testDemo(); // Private method cannot be used

jt.numberOfstuds = 5; // Private variable cannot be used

jt.showDemo();//runs properly

}

}

Protected:

Protected has the same scope as that of private access specifier except that the elements declared as protected can be accessed in the child class of a parent class in which they are declared. It is a useful access specifier for defining the inheritance concept.

We can use protected access modifier when we want to use variables of parent class to be accessed only in the child class and nowhere else.

Consider the below example:

class JavaTraining {

protected int numberOfstuds=10;//protected variable

public void showDemo() {

System.out.println(“The variable value is ” + numberOfstuds);

}

}

class JavaTraining1 extends JavaTraining{

// child class which inherits

// the properties of JavaTraining class

}

public class JavaTraining2{

public static void main(String[] args) {

JavaTraining1 jt1 = new JavaTraining1();

jt1.showDemo();  // runs properly

jt1. numberOfstuds = 45; // runs properly

}

}

Default:

Default is the access specifier which gets automatically applied for the elements for which no access modifier has been specified. The elements which are given the default access control are not accessible across the package, which means that you cannot access a default variable or method of one package in another package.

For Example:

If suppose there’s a package called training with a default variable “numberOfStuds”

package training;

class TrainingDemo {

default int numberOfStuds = 40;//default variable

}

Now if another package called javatraining wants to use “numberOfStuds”, it cannot do that as the variable is default and hence, not accessible in another package.

package javatraining;

import training.TrainingDemo;

class TrainingExample {

public static void main(String[] args){

TrainingDemo td = new TrainingDemo ();

td. numberOfStuds = 67;  //It is not possible.

}

}

Hence, access modifiers are essential components of a java program which define the structure and the way how the elements like variables, method etc will be accessed across the packages, classes and all round in the program. One should join a good Core Java training course in order to get a better understanding of these access control concepts in Java through practical training under the guidance of experts.

TOPS Technologies is a leading Java training institute in Ahmedabad offering excellent job-oriented Java courses with guaranteed placements for the freshers and students. The institute is known for training on latest technologies and has an effective java training program that includes training on current technologies of Java like J2EE, Spring, Hibernate framework etc.

Leave a Reply

Your email address will not be published.

Leave a Reply

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