Java Tutorials: Access Modifiers in Java | private, default, protected, public

preview_player
Показать описание
Private: It can be applied to Field, Method, Constructor, Nested class. Can't be applied to class
- Private Fields Only visible in same class. Can be used via getter/setter in some cases.
- Private method are required, when they're not required for other classes. Only for internal purpose
- Private constructor can not be called from outside of class, though it can be called from other constructor or method in same class.

Default & Public can be applied to Class, Field, Method, Constructor, Nested class, while private & protected can't applied to class
Default: The default Java access modifier is declared by not writing any access modifier at all.
- The default access modifier means that code inside the class itself as well as code inside classes in the same package as this class,
can access the class, field, constructor or method which the default access modifier is assigned to.
- Therefore, the default access modifier is also sometimes referred to as the package access modifier.
- Subclasses cannot access methods and member variables (fields) in the superclass, if they these methods and fields are marked with the
default access modifier, unless the subclass is located in the same package as the superclass.

Protected: The protected access modifier provides the same access as the default access modifier, with the addition that subclasses
can access protected methods and member variables (fields) of the superclass.
- This is true even if the subclass is not located in the same package as the superclass.

Public: The Java access modifier public means that all code can access the class, field, constructor or method, regardless
of where the accessing code is located. The accessing code can be in a different class and different package.

Class Access Modifier: It is important to keep in mind that the Java access modifier assigned to a Java class takes precedence over
any access modifiers assigned to fields, constructors and methods of that class.
If the class is marked with the default access modifier, then no other class outside the same Java package can access that class,
including its constructors, fields and methods. It doesn't help that you declare these fields public, or even public static.

Interface Access modifier: Java interfaces are meant to specify fields and methods that are publicly available in classes that implement
the interfaces. Therefore you cannot use the private and protected access modifiers in interfaces.
Fields and methods in interfaces are implicitly declared public if you leave out an access modifier,
so you cannot use the default access modifier either (no access modifier).

Access Modifiers and Inheritance
When you create a subclass of some class, the methods in the subclass cannot have less accessible access modifiers assigned to them
than they had in the superclass. For instance, if a method in the superclass is public then it must be public in the subclass too,
in case the subclass overrides the method. If a method in the superclass is protected then it must be either protected or public in the subclass.

While it is not allowed to decrease accessibility of an overridden method, it is allowed to expand accessibility of an overridden method.
For instance, if a method is assigned the default access modifier in the superclass, then it is allowed to assign the overridden method in the subclass the public access modifier.

Do watch video for more info.

This problem is similar to following:
access modifiers in java,
access modifier,
private,
protected,
default,
public,
class modifier,
java tutorial,
coding simplified,
java

CHECK OUT CODING SIMPLIFIED

I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 300+ videos. My account is Partner Verified.

★☆★ VIEW THE BLOG POST: ★☆★

★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★

★☆★ SEND EMAIL At: ★☆★
Рекомендации по теме