Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...


what is the Diff. between Access Specifiers and Access
Modifiers?

Answers were Sorted based on User's Feedback



what is the Diff. between Access Specifiers and Access Modifiers?..

Answer / ejp

This is incredible. Where do you people get this nonsense?
See replies #13,17,19,21,23. THERE IS NO SUCH THING AS AN
ACCESS SPECIFIER IN JAVA. See the JLS. If somebody thinks
otherwise kindly post your source for this misconception.

Is This Answer Correct ?    3 Yes 0 No

what is the Diff. between Access Specifiers and Access Modifiers?..

Answer / ejp

Still wrong. Read the thread. At this point anybody who
still thinks there is such a thing as an access specifier in
Java needs to produce a definition of it from the JLS. There
isn't one.

Or else just stop it.

Is This Answer Correct ?    6 Yes 4 No

what is the Diff. between Access Specifiers and Access Modifiers?..

Answer / ejp

'The main difference between access specifiers and
modifiers' is that access specifiers DO NOT EXIST IN JAVA.

Period.

Is This Answer Correct ?    2 Yes 0 No

what is the Diff. between Access Specifiers and Access Modifiers?..

Answer / sohil chhabriya

The access to classes, constructors, methods and fields are
regulated using access modifiers i.e. a class can control
what information or data can be accessible by other classes.
To take advantage of encapsulation, you should minimize
access whenever possible.

Java provides a number of access modifiers to help you set
the level of access you want for classes as well as the
fields, methods and constructors in your classes. A member
has package or default accessibility when no accessibility
modifier is specified.

Access Modifiers

1. private
2. protected
3. default
4. public

public access modifier

Fields, methods and constructors declared public (least
restrictive) within a public class are visible to any class
in the Java program, whether these classes are in the same
package or in another package.

private access modifier

The private (most restrictive) fields or methods cannot be
used for classes and Interfaces. It also cannot be used for
fields and methods within an interface. Fields, methods or
constructors declared private are strictly controlled, which
means they cannot be accesses by anywhere outside the
enclosing class. A standard design strategy is to make all
fields private and provide public getter methods for them.

protected access modifier

The protected fields or methods cannot be used for classes
and Interfaces. It also cannot be used for fields and
methods within an interface. Fields, methods and
constructors declared protected in a superclass can be
accessed only by subclasses in other packages. Classes in
the same package can also access protected fields, methods
and constructors as well, even if they are not a subclass of
the protected member’s class.

default access modifier

Java provides a default specifier which is used when no
access modifier is present. Any class, field, method or
constructor that has no declared access modifier is
accessible only by classes in the same package. The default
modifier is not used for fields and methods within an interface.

Below is a program to demonstrate the use of public,
private, protected and default access modifiers while
accessing fields and methods. The output of each of these
java files depict the Java access specifiers.

The first class is SubclassInSamePackage.java which is
present in pckage1 package. This java file contains the Base
class and a subclass within the enclosing class that belongs
to the same class as shown below.

package pckage1;

class BaseClass {

public int x = 10;
private int y = 10;
protected int z = 10;
int a = 10; //Implicit Default Access Modifier
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
private int getY() {
return y;
}
private void setY(int y) {
this.y = y;
}
protected int getZ() {
return z;
}
protected void setZ(int z) {
this.z = z;
}
int getA() {
return a;
}
void setA(int a) {
this.a = a;
}
}

public class SubclassInSamePackage extends BaseClass {

public static void main(String args[]) {
BaseClass rr = new BaseClass();
rr.z = 0;
SubclassInSamePackage subClassObj = new
SubclassInSamePackage();
//Access Modifiers - Public
System.out.println("Value of x is : " + subClassObj.x);
subClassObj.setX(20);
System.out.println("Value of x is : " + subClassObj.x);
//Access Modifiers - Public
// If we remove the comments it would result in a
compilaton
// error as the fields and methods being accessed are
private
/* System.out.println("Value of y is : "+subClassObj.y);

subClassObj.setY(20);

System.out.println("Value of y is : "+subClassObj.y);*/
//Access Modifiers - Protected
System.out.println("Value of z is : " + subClassObj.z);
subClassObj.setZ(30);
System.out.println("Value of z is : " + subClassObj.z);
//Access Modifiers - Default
System.out.println("Value of x is : " + subClassObj.a);
subClassObj.setA(20);
System.out.println("Value of x is : " + subClassObj.a);
}
}

Output

Value of x is : 10
Value of x is : 20
Value of z is : 10
Value of z is : 30
Value of x is : 10
Value of x is : 20

The second class is SubClassInDifferentPackage.java which is
present in a different package then the first one. This java
class extends First class (SubclassInSamePackage.java).

import pckage1.*;

public class SubClassInDifferentPackage extends
SubclassInSamePackage {

public int getZZZ() {
return z;
}

public static void main(String args[]) {
SubClassInDifferentPackage subClassDiffObj = new
SubClassInDifferentPackage();
SubclassInSamePackage subClassObj = new
SubclassInSamePackage();
//Access specifiers - Public
System.out.println("Value of x is : " + subClassObj.x);
subClassObj.setX(30);
System.out.println("Value of x is : " + subClassObj.x);
//Access specifiers - Private
// if we remove the comments it would result in a
compilaton
// error as the fields and methods being accessed are
private
/* System.out.println("Value of y is : "+subClassObj.y);

subClassObj.setY(20);

System.out.println("Value of y is : "+subClassObj.y);*/
//Access specifiers - Protected
// If we remove the comments it would result in a
compilaton
// error as the fields and methods being accessed are
protected.
/* System.out.println("Value of z is : "+subClassObj.z);

subClassObj.setZ(30);

System.out.println("Value of z is : "+subClassObj.z);*/
System.out.println("Value of z is : " +
subClassDiffObj.getZZZ());
//Access Modifiers - Default
// If we remove the comments it would result in a
compilaton
// error as the fields and methods being accessed are
default.
/*

System.out.println("Value of a is : "+subClassObj.a);

subClassObj.setA(20);

System.out.println("Value of a is : "+subClassObj.a);*/
}
}

Output

Value of x is : 10
Value of x is : 30
Value of z is : 10

The third class is ClassInDifferentPackage.java which is
present in a different package then the first one.

import pckage1.*;

public class ClassInDifferentPackage {

public static void main(String args[]) {
SubclassInSamePackage subClassObj = new
SubclassInSamePackage();
//Access Modifiers - Public
System.out.println("Value of x is : " + subClassObj.x);
subClassObj.setX(30);
System.out.println("Value of x is : " + subClassObj.x);
//Access Modifiers - Private
// If we remove the comments it would result in a
compilaton
// error as the fields and methods being accessed are
private
/* System.out.println("Value of y is : "+subClassObj.y);

subClassObj.setY(20);

System.out.println("Value of y is : "+subClassObj.y);*/
//Access Modifiers - Protected
// If we remove the comments it would result in a
compilaton
// error as the fields and methods being accessed are
protected.
/* System.out.println("Value of z is : "+subClassObj.z);

subClassObj.setZ(30);

System.out.println("Value of z is : "+subClassObj.z);*/
//Access Modifiers - Default
// If we remove the comments it would result in a
compilaton
// error as the fields and methods being accessed are
default.
/* System.out.println("Value of a is : "+subClassObj.a);

subClassObj.setA(20);

System.out.println("Value of a is : "+subClassObj.a);*/
}
}

Output

Value of x is : 10
Value of x is : 30

Is This Answer Correct ?    3 Yes 1 No

what is the Diff. between Access Specifiers and Access Modifiers?..

Answer / ejp

Still wrong. The difference is that access specificers don't
exist. Period.

Is This Answer Correct ?    4 Yes 3 No

what is the Diff. between Access Specifiers and Access Modifiers?..

Answer / vikram singh rawat

Access Specifier are like:
1) public
2) private
3) protected
4) Default

Access Specifier or modifiers in java control the access
and visibility of classes and class members.

Access Modifiers are like:
1) Volatile
2) Native
3) Transient

Field and method modifiers are keywords used to identify
fields and methods that need to be declared for controlling
access to users. Some of these can be used along with
public and protected keywords.

Is This Answer Correct ?    3 Yes 2 No

what is the Diff. between Access Specifiers and Access Modifiers?..

Answer / ejp

Wrong again.

Is This Answer Correct ?    2 Yes 1 No

what is the Diff. between Access Specifiers and Access Modifiers?..

Answer / ejp

#31 doesn't answer the question, and repeats the same
confusion expressed in the question. There is NO SUCH THING
as an access specifier in Java, and therefore nothing for
the question to be about.

Is This Answer Correct ?    2 Yes 1 No

what is the Diff. between Access Specifiers and Access Modifiers?..

Answer / jessie

Access specifiers are public,private and protected.By default it is default.Access specifiers provides the scope and visibility of class,method and variable.
Access modifiers are those(static,final,abstract,transient..etc) which modifies the visibility provided by access specifiers...

Is This Answer Correct ?    1 Yes 0 No

what is the Diff. between Access Specifiers and Access Modifiers?..

Answer / santosh kumar(siddhu)

Acess specifer gives some boundry to access.
these r 1)Public(boundry:any where with in package and
outside of package)
2)Private(Boundry:within class scope only)
3)Defult(Boundry:within package scope only)
4)Protected(Boundry:within package scope and
outside subclass of this package class)

Acess modifier change the properties,Behaviour
these are
Public
Abstract
Final
Static
Volatile
Synchronized
Transient
Native

Is This Answer Correct ?    5 Yes 5 No

Post New Answer



More Core Java Interview Questions

What is a null check?

0 Answers  


What is an event?

2 Answers  


what is the need of the Ajax?

5 Answers   iFlex,


What is difference between “==” and equals()?

1 Answers   IBM,


Can a final variable be initialized in constructor?

0 Answers  






Write an algorithm program in java for the following question.. 1) S is a set of integers.X is an integer obtained by sum of two digits in S. Write logic for whether or not the X is from the S. The time of algorithm should not exceed o(n logn).

0 Answers  


What are the special characters?

0 Answers  


What do you mean by collectors in java 8?

0 Answers  


What is the major drawback of internal iteration over external iteration?

0 Answers  


what is multithreading?

5 Answers   Virtusa,


What is core java used for?

0 Answers  


What is OOP Language?

0 Answers   Atos Origin,






Categories