• Tidak ada hasil yang ditemukan

Access Modifiers in Java There are two types of modifers in Java

N/A
N/A
Protected

Academic year: 2025

Membagikan "Access Modifiers in Java There are two types of modifers in Java"

Copied!
5
0
0

Teks penuh

(1)

Access Modifiers in Java

There are two types of modifers in Java: access modifers and non-access modiferss

The access modifers in Java specifes the accessibility or scope of a feldd methodd constructord or classs We can change the access level of feldsd constructorsd methodsd and class by applying the access modifer on its

There are four types of Java access modifers:

Private: The access level of a private modifer is only within the classs It cannot be accessed from outside the classs

Default: The access level of a default modifer is only within the packages It cannot be accessed from outside the packages If you do not specify any access leveld it will be the defaults

Protected: The access level of a protected modifer is within the package and outside the package through child classs If you do not make the child classd it cannot be accessed from outside the packages Public: The access level of a public modifer is everywheres It can be accessed from within the classd outside the classd within the package and outside the packages

There are many non-access modifersd such as statcd abstractd synchronizedd natved volatled transientd etcs Hered we are going to learn the access modifers onlys

Understanding Java Access Modifiers

Let's understand the access modifers in Java by a simple tables

Access Modifer

within class

within package

outside package by subclass only

outside package

Private Y N N N

Default Y Y N N

Protected Y Y Y N

Public Y Y Y Y

1) Private

The private access modifer is accessible only within the classs Simple example of private access modifier

(2)

In this exampled we have created two classes A and Simples A class contains private data member and private methods We are accessing these private members from outside the classd so there is a compile- tme errors

class A {

private int data=40;

private void msg() {

Systemsoutsprintln("Hello java");

} }

public class Simple {

public statc void main(String args[]) {

A obj=new A();

Systemsoutsprintln(objsdata);//Compile Time Error objsmsg();//Compile Time Error

} }

2) Default

If you don't use any modiferd it is treated as default by defaults The default modifer is accessible only within packages It cannot be accessed from outside the packages It provides more accessibility than privates Butd it is more restrictve than protectedd and publics

Example of default access modifier

class A {

void msg() //default access specifer {

Systemsoutsprintln("Hello");

(3)

} }

class B extends A {

public statc void main(String args[]) {

A obj = new A();

objsmsg();

} }

In the above exampled the scope of class A and its method msg() is default so it can be accessed from outside the packages

3) Protected

The protected access modifer is accessible within package and outside the package but through inheritance onlys

The protected access modifer can be applied on the data memberd method and constructors It can't be applied on the classs

It provides more accessibility than the default modifers Example of protected access modifier

In this exampled we have created the two packages pack and mypacks The A class of pack package is publicd so can be accessed from outside the packages But msg method of this package is declared as protectedd so it can be accessed from outside the class only through inheritances

//save by Asjava package pack;

public class A {

protected void msg() {

Systemsoutsprintln("Hello");

}

(4)

}

//save by Bsjava package mypack;

import packs*;

class B extends A {

public statc void main(String args[]) {

B obj = new B();

objsmsg();

} }

Output:Hello

4) Public

The public access modifer is accessible everywheres It has the widest scope among all other modiferss Example of public access modifer

//save by Asjava package pack;

public class A {

public void msg() {

Systemsoutsprintln("Hello");

} }

//save by Bsjava

(5)

package mypack;

import packs*;

class B {

public statc void main(String args[]) {

A obj = new A();

objsmsg();

} }

Output:Hello

Referensi

Dokumen terkait

Based on the title of the research, this study is limited and focused on the use of pre-noun modifiers and the errors in pre-noun modifiers made by the students of the

There were two theories that used in this research such as the first theory proposed by Knickerbocker and Reninger (1974) entitled Interpreting Literature us to find out types of

There are two types of stylistic devices, the first types of stylistic devices based on the interaction of lexical and contextual meaning consist of metaphor, metonymy, antonomasia,

The survey was carried out based on the following assumptions: a that the infrastructure problems hindered access and use of the Internet in Dar es Salaam; b there existed government

Results and Discussions All the three proposed CAC schemes using limited access probability have been evaluated in comparison to the conven- tional trunk reservation technique for

In Indonesia, especially Java, there are five species recorded in the book Flora of Java [5] among others; Ziziphus javanensis Blume, Ziziphus horsfieldii Miq., Ziziphus rotundifolia

In relation with the material aspect or the aspect of the accessibility of children with special needs having education at inclusive primary school, there are problems concerning the

The document explores the role of community learning centers in combating poverty through non-formal education in Java,