1. Overview
In this tutorial, We'll learn about Access Modifiers in Java. Java provides the 4 types of access modifiers for class, constructors, methods and instance variables.
These four access modifiers changes complete accessibility at various levels.
We will explore on each modifier as following.
A. default - No keyword is required
B. private
C. protected
D. public
This is a very basic in core java programming. If you understand this, you can crack any code in java application.
2. Default Access Modifier
If no keyword is specified at any level (class, constructors, methods and instance variables) then it is limited to its package. To apply default access modifier, we do not need to explicitly define on class or any.
A method without any access specifier then it is accessible with in the same package. Same rule is applicable for instance variable too.
See the below example at all levels of a class for default.
package com.blog.java.w3schools.default classs Trade{ int tradeId = 12345; int getTradeId(){ return tradeId; }
If we try to access from outside of package then will get compile time error. Because, class Trade is not visible to outside packages.
In the above example, field tradeId, method getTradeId and class Trade are default because nothing is specified. So, all are limited to package.
Note: With respect to the Interfaces, Fields are by default public static final even though not specified anything and methods are public abstract.
3. private access modifier
private is not applicable to class and interface. Applicable to only methods, instance variables and constructors.
Once method or instance variable declared as private then it is accessible on within class. This is the highly restricted access modifier. No other has restricted like this.
class Studnet { private int id studentId = 200; public int getStudentId() return studentId; } }
Take a look at above example where studentId is declared as private. That means there is no way to access directly with Studnet object as below.
Studnet stu = new Studnet(); int sid = stu.studentId;
This will though complete time error.
Accessing private variable is only using public getter method.
int stuId = stu.getStudentId();
If constructor is delcared as private then object is not allowed to created outside the class. This is important step in Singleton design pattern in java.
class MySingletonClass { private MySingletonClass(){ } // singleton code here }
main() method should be public. If we make it private then JVM can not find the main method. We can not run this program.
4. protected access modifier
Comparatively higher access to default modifier but down to the public.
This can be used on variables, methods and constructors.
Protected variables, methods and constructors are accessed by all sub classes outside the package and all classes with in the same protected member's package.
We cannot apply protected to the class and interfaces. And also methods inside the interface can not be declared as protected.
SuperClass:
package com.blog.java.w3schools.same.pakage; class SuperClass { protected void process() { // processing generic logic } }
In the above examples, process() method is declared as protected in SuperClass class.
Now created a new sub class in different package.
Subclass:
package com.blog.java.w3schools.another.pakage; class SubClass extends SuperClass{ protected void processSubClass() { process(); // processing sub class logic with generic logic from parent class. } }
Observe the above subclass protected method process() is directly accessible in child class.
Note: Only protected methods, constructors and variables are accessible in child class outside package. default and private are not allowed to access outside the package.
5. public access modifier
public is the wider access modifier among all four in java. Anything is declared as public is allowed to access from anywhere from the application which is said as global access modifier.
This can be used on Class, Interface, Method, Constructor and instance variables. Once public is used at any level then we can access from any where such as within package, outside package or through inheritance.
But, We must have to import the package if accessing from outside package.
Example public class:
public class JavaGlobalClass{ public boolean isGloble = true; public JavaGlobalClass(){ // Creating instance. } public boolean getGlobalStatus(){ return isGloble; } }
In the above class, All are declared as public. In other words, visible to all others classes in the java world.
Public Example on interface: public interface MyPublicInterface { public static final double PI = 3.14; public abstract Student getTopStudent(); }
6. Access Modifier's in Inheritance
consider the following class a subclass.
package com.blog.java.w3schools.another.pakage; class SubClass extends SuperClass{ protected void process() { // processing sub class releated implementation. } }
Rules
- If method is declared as public in super class then implementation in the child class implementation must be public.
- If method is declared as protected in super class then implementation in the child class must be public or same level as protected.
- If method is declared as default (no keyword) in super class then implementation in the child class must be public or protected or default. This child class must be in the same package because method is default in super class.
- private methods are accessible to the class and not from outside of class. So, they do not participate in overriding.
7. Conclusion
In this article, We've seen What are Access Level Modifiers in Java. This is very basic and important topic in Java Fundamental programming concepts.
We have covered in detail on public, protected, default (no keyword required) and private modifiers with example code.
And also seen aspect of Inheritance in case of protected and public using child and super class relationship.
Example code snippets shown in this article is available over GitHub.
No comments:
Post a Comment
Please do not add any spam links in the comments section.