=== What is Modifier in Java? ===
- A modifier in java is a keyword which we add to those definitions that we need to change their meaning.
- In other words, a modifier limits the visibility of classes, fields, constructors, or methods in the Java program.
- The functionality of members of a class or a class itself can be protected from other parts of the program by the presence or absence of modifiers.
0:00 - Introduction
01:26 - Access Modifiers in Java
02:20 - Private Access Modifier
06:42 - Public Access Modifier
10:06 - Protected Access Modifier
15:52 - Default Access Modifier
=== Java language provides a total of 12 modifiers ===
[1] public
[2] private
[3] protected
[4] default
[5] final
[6] synchronized
[7]static
[8] abstract
[9] native
[10] strictfp
[11] transient
[12] volatile
=== Twelve modifiers in java can be divided into two categories ===
[1] Access modifiers
[2] Non-access modifiers
- In this tutorial, we will focus only on access modifiers and in the next tutorial, we will cover non-access modifiers.
=== Access Modifiers in Java ===
- Access modifiers/specifiers in java define the boundary for accessing members of a class and a class itself.
- In other words, access modifiers are those modifiers that are used to restrict the visibility (accessibility) of classes, fields, methods, or constructors.
- In short, the accessibility/visibility of data depends on the access modifiers. The access modifiers are also known as visibility modifiers.
=== Java provides four explicit access modifiers in object-oriented programming languages ===
[1] private,
[2] public
[3] protected
[4] default
[1] Private access modifier :
- The scope of private modifier is limited to the class only.
i) Private Data members and methods are only accessible within the class
ii) Class and Interface cannot be declared as private
iii) If a class has private constructor then you cannot create the object of that class from outside of the class
[2] Public access modifier :
- The members, methods and classes that are declared public can be accessed from anywhere.
- This modifier doesn’t put any restriction on the access.
- Public members of a class can be inherited to any subclass
[3] Protected Access Modifier :
- Protected data member and method are only accessible by the classes of the same package and the subclasses present in any package.
- You can also say that the protected access modifier is similar to default access modifier with one exception that it has visibility in sub classes.
- Classes cannot be declared protected. This access modifier is generally used in a parent child relationship.
- If we make constructor as protected then we can create the subclass of that class within the same package but not outside the package.
- Protected access modifier can be applied to instance variables, local variables, constructors, methods, inner classes but not the outer class.
[4] Default access modifier :
- When we do not mention any access modifier, it is called default access modifier.
- The scope of this modifier is limited to the package only.
- This means that if we have a class with the default access modifier in a package, only those classes that are in this package can access this class.
- No other class outside this package can access this class. Similarly, if we have a default method or data member in a class, it would not be visible in the class of another package.
#LearningandTeachingCoding