answersLogoWhite

0

What is public access specifier?

Updated: 8/10/2023
User Avatar

Wiki User

12y ago

Best Answer

An Access Modifier is a key word in java that determines what level of access or visibility a particular java variable/method or class has. There are 4 basic access modifiers in java. They are:

1. Public

2. Protected

3. Default and

4. Private

Private is the most restrictive access modifier whereas public is the least restrictive. Default is the access protection you get when you do not specifically mention an access modifier to be used for a java object.

Java programming does not run by just a single piece of class that has the whole functionality. You have hundreds of classes that interact with one another, passing data between them and returning output to the user of the system. So it is very important for members of one class to access members of another. Here members may refer to variables, methods and even classes. So, this is where the access modifiers come into picture. The modifier associated with every member of the class determines what level of visibility that member has.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

In a class definition, the keyword "public:" denotes that all the following members (both variables and methods) are accessible to all consumers of the class. The access specifier remains in force until another access specifier is encountered, however the same specifier may be repeated throughout the definition, and they may appear in any order.

Note that a class always has full access to all of its own members, regardless of the access specifiers. However, class members may be public, protected or private. This determines how accessible those members are to the consumers of the class. In other words, they determine how much of the interface you expose outside of the class.

Unless otherwise specified, all class members are private. Private members are only accessible to the class itself, and friends of the class (whether those friends are entire classes, individual class methods, or individual functions).

Protected members are similar to private members, but are also accessible to derived classes.

Public members are accessible to all consumers of the class.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

This access modifier is actually working like protectedor internal, means only accessible by the derived classes (protected), or the one within the same assembly (internal).

Personally, I have not had a chance to use protected internal. I've been asked this question many times, yet when I persisted to ask for an example of the need to use it, thus far, all the examples were needed because of design flaws. (once the flaw being corrected, there is no use of it). It could be like the public modifier being applied to a data member, it is there does not mean we should use it. (declare a data member as public violates one of the OO principles - encapsulation. Another way to look at it, you expose your bank account to public!!)

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Protected access is the exact same as private access, but also permits access to derivatives of the class. But outside of those derivatives, the member is effectively private.

Consider the following example:

#include <iostream>

class base

{

private: // accessible to class members and to friends of the class.

int m_private;

protected: // same as private, but also accessible to derivatives

int m_protected;

public: // accessible to all

int m_public;

public: // constructor

base(){

m_public = 1; // ok

m_protected = 2; // ok

m_private = 3; // ok

}

};

class derived : public base

{

public: // constructor

derived(){

m_public = 4; // ok

m_protected = 5; // ok

m_private = 6; // access denied

}

};

int main()

{

base b;

b.m_public = 7; // ok

b.m_protected = 8; // access denied

b.m_private = 9; // access denied

derived d;

d.m_public = 10; // ok

d.m_protected = 11; // access denied

d.m_private = 12; // access denied

return( 0 );

}

As you can see, derived has access to all but the private members of base. However, main, which is outside of both classes, only has access to the public members.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The protected access specifier means that the member data or method can only be accessed from within the class, or from within a class that derives the class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is public access specifier?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are the various access specifiers?

Three types of access specifier private , public ,protected


Default access specifier in java?

There is no such thing as an access specifier in Java. There are access modifiers.The default access modifier if unspecified is to allow access to classes in the current package only, except within an interface where the default is 'public'.


Explain the significance of public and protected and private access specifiers in inheritance?

These are all access modifiers in Java. a. Public - these are accessible anywhere. This is the least restrictive access specifier. b. Private - these are accessible only inside the declaring class. This is the most restrictive access specifier. c. Protected - these are in between public and private. These are accessible to all classes that inherit this class d. Package - this is the default access specifier. These are accessible to all classes that are present in the same package as the contained class.


What kinds of access can a class member have?

Public, protected and private access members.


What is meant by private access in c plus plus?

It isn't. Private is the default access for class members. For struct members, the default access is public. Aside from default access, a class and a struct serve the same purpose; to define a class. As such, the following class definitions are equivalent: class X { int a; }; struct Y { private: int b; }; Typically, we use a struct to define simple data types with trivial construction and use class for more complex data types, often to encapsulate an invariant or to acquire a resource, hiding the implementation details from consumers using private access.

Related questions

What is an access specifier do?

There is no such thing as an access specifier in Java. There are access modifiers. They specify the access level of the item they modify: public, private, protected.


What is the use of public access specifier?

use of public access specifier iswe can access the class members(methods,variables) out side the class using class reference


Which is public to all access class or struct or function or variable?

The default access specifier for a class is private. The default access specifier for a struct is public. It does not matter if it is a function or a variable.


Where do you define the access specifier for a java method properties?

We define the access specifier of a function at the place of its method signature(The place we write the method's name).for example,public void sample(){}here "public" is the access specifier of function name-sample.


What are the various access specifiers?

Three types of access specifier private , public ,protected


What do you mean by access specifier in c?

There are no access specifiers in C. All functions and data are public.


What is the default access specifier in java?

There is no such thing as an access specifier in Java. There are access modifiers.The default access modifier if unspecified is to allow access to classes in the current package only, except within an interface where the default is 'public'


What is the default access specifier of Java?

There is no such thing as an access specifier in Java. There are access modifiers.The default access modifier if unspecified is to allow access to classes in the current package only, except within an interface where the default is 'public'.


Default access specifier in java?

There is no such thing as an access specifier in Java. There are access modifiers.The default access modifier if unspecified is to allow access to classes in the current package only, except within an interface where the default is 'public'.


Explain the significance of public and protected and private access specifiers in inheritance?

These are all access modifiers in Java. a. Public - these are accessible anywhere. This is the least restrictive access specifier. b. Private - these are accessible only inside the declaring class. This is the most restrictive access specifier. c. Protected - these are in between public and private. These are accessible to all classes that inherit this class d. Package - this is the default access specifier. These are accessible to all classes that are present in the same package as the contained class.


What is an access specifier?

An access specifier is a keyword applied to a variable, or method, which indicates which parts of the program are permitted to access it.


What is the difference between access specifier and access modifier?

An access modifier is another name for an access specifier, which in object-orientated software is a keyword applied to a variable which indicates which other parts of the programme are permitted to access it.