So I just started reading a Java book and wondered; which access specifier is the default one if none is specified?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Public is a keyword that is used as an access modifier for methods and variables. A variable (field) or a method declared as public is visible to and may be accessed by all classes defined in different packages.
The public keyword is used in many object-oriented programming languages (OOPL), including C++, Java, C# and Visual Basic.NET (VB.NET). A public member is loosely bound to a class and less restrictive than a private member. Variables or fields that are declared public have no accessibility restrictions. A private member, however, is only visible in its class.
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.
Others access modifiers
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.
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.
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. Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.
If a class do not have any access modifier it will be treated under default access modifier.
In object oriented programming languages, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:
A language mechanism for restricting direct access to some of the object's components A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.
..
..
..
..
https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)
https://www.quora.com/In-Java-what-is-the-difference-between-public-class-and-class https://www.techopedia.com/definition/24018/public-java
https://www.geeksforgeeks.org/access-modifiers-java/
If no access specifier is given, it's package-level access (there is no explicit specifier for this) for classes and class members. Interface methods are implicitly public.
The default specifier depends upon context.
For classes, and interface declarations, the default is package private. This falls between protected and private, allowing only classes in the same package access. (protected is like this, but also allowing access to subclasses outside of the package.)
For interface members (fields and methods), the default access is public. But note that the interface declaration itself defaults to package private.
If we then have the declaration
Classes using MyInterface2 can then see field1 and method1 from the super interface, because they are public, even though they cannot see the declaration of MyInterface itself.
See here for more details. The default is none of private/public/protected, but a completely different access specification. It's not widely used, and I prefer to be much more specific in my access definitions.
Update Java 8 usage of
default
keyword: As many others have noted The default visibility (no keyword)Not to be confused with the new Java 8 feature (Default Methods) that allows an interface to provide an implementation when its labeled with the
default
keyword.See: Access modifiers
First of all let me say one thing there is no such term as "Access specifier" in java. We should call everything as "Modifiers". As we know that final, static, synchronised, volatile.... are called as modifiers, even Public, private, protected, default, abstract should also be called as modifiers . Default is such a modifiers where physical existence is not there but no modifiers is placed then it should be treated as default modifiers.
To justify this take one example:
Output will be:
Hello Java
Now change public to private and see what compiler error you get: It says "Modifier private is not allowed here" What conclusion is someone can be wrong or some tutorial can be wrong but compiler cannot be wrong. So we can say there is no term access specifier in java everything is modifiers.