{"id":1192,"date":"2022-02-25T16:35:24","date_gmt":"2022-02-25T16:35:24","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1192"},"modified":"2022-02-25T16:35:24","modified_gmt":"2022-02-25T16:35:24","slug":"java-access-modifiers","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/25\/java-access-modifiers\/","title":{"rendered":"Java Access Modifiers"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Java Access Modifier, its types, and how to use them with the help of examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"introduction\">What are Access Modifiers?<\/h2>\n\n\n\n<p>In Java, access modifiers are used to set the accessibility (visibility) of classes, interfaces, variables, methods, constructors, data members, and the setter methods. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n    public void method1() {...}\n\n   private void method2() {...}\n}<\/code><\/pre>\n\n\n\n<p>In the above example, we have declared 2 methods: method1() and method2(). Here,<\/p>\n\n\n\n<ul><li><var>method1<\/var>&nbsp;is&nbsp;<code>public<\/code>&nbsp;&#8211; This means it can be accessed by other classes.<\/li><li><var>method2<\/var>&nbsp;is&nbsp;<code>private<\/code>&nbsp;&#8211; This means it can not be accessed by other classes.<\/li><\/ul>\n\n\n\n<p>Note the keyword&nbsp;<code>public<\/code>&nbsp;and&nbsp;<code>private<\/code>. These are access modifiers in Java. They are also known as visibility modifiers.<\/p>\n\n\n\n<p><strong>Note<\/strong>: You cannot set the access modifier of getters methods.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Access Modifier<\/h2>\n\n\n\n<p>Before you learn about types of access modifiers, make sure you know about\u00a0Java Packages.<\/p>\n\n\n\n<p>There are four access modifiers keywords in Java and they are:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td><strong>Modifier<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td>Default<\/td><td>declarations are visible only within the package (package private)<\/td><\/tr><tr><td>Private<\/td><td>declarations are visible within the class only<\/td><\/tr><tr><td>Protected<\/td><td>declarations are visible within the package or all subclasses<\/td><\/tr><tr><td>Public<\/td><td>declarations are visible everywhere<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"default-access-modifiers\">Default Access Modifier<\/h2>\n\n\n\n<p>If we do not explicitly specify any access modifier for classes, methods, variables, etc, then by default the default access modifier is considered. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package defaultPackage;\nclass Logger {\n    void message(){\n        System.out.println(\"This is a message\");\n    }\n}<\/code><\/pre>\n\n\n\n<p>Here, the&nbsp;<var>Logger<\/var>&nbsp;class has the default access modifier. And the class is visible to all the classes that belong to the&nbsp;<var>defaultPackage<\/var>&nbsp;package. However, if we try to use the&nbsp;<var>Logger<\/var>&nbsp;class in another class outside of defaultPackage, we will get a compilation error.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"private-access-modifiers\">Private Access Modifier<\/h2>\n\n\n\n<p>When variables and methods are declared&nbsp;<code>private<\/code>, they cannot be accessed outside of the class. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Data {\n    \/\/ private variable\n    private String name;\n}\n\npublic class Main {\n    public static void main(String&#91;] main){\n\n        \/\/ create an object of Data\n        Data d = new Data();\n\n        \/\/ access private variable and field from another class\n        d.name = \"Programiz\";\n    }\n}<\/code><\/pre>\n\n\n\n<p>In the above example, we have declared a private variable named&nbsp;<var>name<\/var>. When we run the program, we will get the following error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Main.java:18: error: name has private access in Data\n        d.name = \"Programiz\";\n         ^<\/code><\/pre>\n\n\n\n<p>The error is generated because we are trying to access the private variable of the&nbsp;<var>Data<\/var>&nbsp;class from the&nbsp;<var>Main<\/var>&nbsp;class.<\/p>\n\n\n\n<p>You might be wondering what if we need to access those private variables. In this case, we can use the getters and setters method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Data {\n    private String name;\n\n    \/\/ getter method\n    public String getName() {\n        return this.name;\n    }\n    \/\/ setter method\n    public void setName(String name) {\n        this.name= name;\n    }\n}\npublic class Main {\n    public static void main(String&#91;] main){\n        Data d = new Data();\n\n        \/\/ access the private variable using the getter and setter\n        d.setName(\"Programiz\");\n        System.out.println(d.getName());\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The name is Programiz<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have a private variable named&nbsp;<var>name<\/var>. In order to access the variable from the outer class, we have used methods:&nbsp;<code>getName()<\/code>&nbsp;and&nbsp;<code>setName()<\/code>. These methods are called getter and setter in Java.<\/p>\n\n\n\n<p>Here, we have used the setter method (<code>setName()<\/code>) to assign value to the variable and the getter method (<code>getName()<\/code>) to access the variable.<\/p>\n\n\n\n<p>We have used\u00a0<code>this<\/code>\u00a0keyword inside the setName() to refer to the variable of the class. <\/p>\n\n\n\n<p><strong>Note<\/strong>: We cannot declare classes and interfaces private in Java. However, the nested classes can be declared private. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"protected-access-modifiers\">Protected Access Modifier<\/h3>\n\n\n\n<p>When methods and data members are declared&nbsp;<code>protected<\/code>, we can access them within the same package as well as from subclasses. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n    \/\/ protected method\n    protected void display() {\n        System.out.println(\"I am an animal\");\n    }\n}\n\nclass Dog extends Animal {\n    public static void main(String&#91;] args) {\n\n        \/\/ create an object of Dog class\n        Dog dog = new Dog();\n         \/\/ access protected method\n        dog.display();\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>I am an animal<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have a protected method named\u00a0<code>display()<\/code>\u00a0inside the\u00a0<var>Animal<\/var>\u00a0class. The\u00a0<var>Animal<\/var>\u00a0class is inherited by the\u00a0<var>Dog<\/var>\u00a0class. <\/p>\n\n\n\n<p>We then created an object&nbsp;<var>dog<\/var>&nbsp;of the&nbsp;<var>Dog<\/var>&nbsp;class. Using the object we tried to access the protected method of the parent class.<\/p>\n\n\n\n<p>Since protected methods can be accessed from the child classes, we are able to access the method of&nbsp;<var>Animal<\/var>&nbsp;class from the&nbsp;<var>Dog<\/var>&nbsp;class.<\/p>\n\n\n\n<p><strong>Note<\/strong>: We cannot declare classes or interfaces&nbsp;<code>protected<\/code>&nbsp;in Java.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"public-access-modifiers\">Public Access Modifier<\/h3>\n\n\n\n<p>When methods, variables, classes, and so on are declared&nbsp;<code>public<\/code>, then we can access them from anywhere. The public access modifier has no scope restriction. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Animal.java file\n\/\/ public class\npublic class Animal {\n    \/\/ public variable\n    public int legCount;\n\n    \/\/ public method\n    public void display() {\n        System.out.println(\"I am an animal.\");\n        System.out.println(\"I have \" + legCount + \" legs.\");\n    }\n}\n\n\/\/ Main.java\npublic class Main {\n    public static void main( String&#91;] args ) {\n        \/\/ accessing the public class\n        Animal animal = new Animal();\n\n        \/\/ accessing the public variable\n        animal.legCount = 4;\n        \/\/ accessing the public method\n        animal.display();\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>I am an animal.\nI have 4 legs.<\/samp><\/code><\/pre>\n\n\n\n<p>Here,<\/p>\n\n\n\n<ul><li>The public class&nbsp;<var>Animal<\/var>&nbsp;is accessed from the&nbsp;<var>Main<\/var>&nbsp;class.<\/li><li>The public variable&nbsp;<var>legCount<\/var>&nbsp;is accessed from the&nbsp;<var>Main<\/var>&nbsp;class.<\/li><li>The public method&nbsp;<code>display()<\/code>&nbsp;is accessed from the&nbsp;<var>Main<\/var>&nbsp;class.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"figure\">Access Modifiers Summarized in one figure<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-access-modifiers-public-private-protected-default_0.jpg\" alt=\"Accessibility of all Access Modifiers in Java\" title=\"Accessibility of all Access Modifiers in Java\"\/><figcaption>Accessibility of all Access Modifiers in Java<\/figcaption><\/figure>\n\n\n\n<p>Access modifiers are mainly used for encapsulation. It can help us to control what part of a program can access the members of a class. So that misuse of data can be prevented.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Java Access Modifier, its types, and how to use them with the help of examples. What are Access Modifiers? In Java, access modifiers are used to set the accessibility (visibility) of classes, interfaces, variables, methods, constructors, data members, and the setter methods. For example, In the above [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[484],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1192"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1192"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1192\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}