What is the use of access modifier

Do I need to use an access modifier in a programming language? if we select all members and methods as private, then what will be the way out?

+3
source share
13 answers

See: Encapsulation (Wikipedia)

Of course, you can declare all participants and methods private, but then you acquired a completely useless class, since you cannot access any of them from another class.

+6
source

Private is used for encapsulation (OOP) to hide the implementation. Public declared methods provide a class interface.

If you declare everything as private, you cannot use methods from other classes. What would the class do with methods that would be completely useless.

+4
source

You need access modifiers to control the accessibility of methods / members. The moral is that you should limit accessibility as affordable as possible, but still you cannot make them private.

Suppose you have a utility class that performs a useful operation for all classes (Ex: logging). In this case, all classes should β€œsee” the utility class and its members. Of course, you cannot make everything here private. This is just one example among hundreds.

+2
source

Access modifiers are used to control the visibility of fields and methods. Often we have good reasons to prohibit the use of a method or field for objects of other classes.

If you just started coding in Java and saved your own code in one class, then you may not need different access modifiers (except for the main method, which should be public ). Access modifiers begin to become important when your project becomes object oriented (= after you implement more than one class).

Here is a pointer to the corresponding article in the official Java tutorial .

+2
source

A private modifier indicates that an item can only be accessed in its class.

If you use all members and members are private, then you cannot have access outside the class.

check this out: http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

+2
source

Access modifiers are used for encapsulation: they allow you to organize your code in packages and classes and have only the β€œofficial” public interface visible from the outside, while hiding implementation details (which you want to do so that you can change it later without telling anyone )

This is especially important (only?) When you release code as a library for other programmers. Even if the code is used only in your own program, it helps to structure larger programs into several packages.

It makes no sense to do everything personal if your program consists of only one class. However, this is a reasonable option: until you think that some other class should call it, make the method private.

The default modifier in Java (if nothing is specified) is protected by the package. This allows you to access the method or field from your own code (that is, code in the same package), but still hides it from anyone else. This is also a reasonable default option or a natural way to upgrade from a private one: before the code outside the package should call it, make the method a protected package.

More noticeable levels are protected (subclasses can see it) and public (everyone can see it). Before you do something public or secure, think about the interface, because it is difficult to change it later.

+2
source

I agree, private great for an empirical field. However, the private modifier denies subclasses access to fields and methods. If all methods and fields were private , subclasses would have nothing to do with their superclasses except their type.

It would also be difficult for one object to be useful for another if all methods are not available. It would be difficult to write even an instance of Object if all methods (including constructors) were private.

The best question to discuss is outside of private and public why there are protected and default access modifiers.

+2
source

You can use the default access, which will make the class suitable for use in the package. You cannot use this private .

+1
source

All about the accessibility of a method or class property. I drew a table, trying to simplify its understanding.

 +----------------------+----------------+-----------------+----------------------+ |Access Modifiers | Same Package | Subclass | Other packages | |----------------------|----------------|-----------------|----------------------| |public | Y | Y | Y | | | | | | |protected | Y | Y | N | | | | | | |no access modifier | Y | N | N | | | | | | |private | Y | N | N | | | | | | +----------------------+----------------+-----------------+----------------------+ 
+1
source

Of course, access modifiers are needed, otherwise they would not be added.

One of the most important features of object-oriented programming and the reuse of your code. If you make everything private, you cannot do it, and you will have to write code again and again! Using public or standard access modifiers will allow you to use your code from other classes.

0
source

Access to classes, constructors, methods and fields is controlled by access modifiers, that is, a class can control what information or data can be accessed by other classes. The main contribution of the access modifier is encapsulation, which is one of the concepts of OOP.

If you make all members and methods private, than you make your code inaccessible to other classes. In general, you cannot reuse the functionality and code that is already available.

0
source

A private modifier indicates that a member can only be accessed in its class. The default modifier is used inside the package. A protected modifier indicates that a member can access both within the package and is inherited at any level.

0
source

All methods and fields are available only in the class, and then do not have access to the external class, even if you inherit or access the object of this class.

Java containing four access specifiers,

 Public Private Protected Default 

Java with five modifiers without access

 static final abstract syncronised volatile 

Source: Tutorial Data - Java Modifier

0
source

Source: https://habr.com/ru/post/908242/


All Articles