Access Modifiers in Java I Java Programming Language I Default I Private I Protected I Public

preview_player
Показать описание
Developed by : Kainat Mehdi
Supervised by : Dr. Ahsanullah Abro
Access Modifiers in Java
Hello Dear Viewers, welcome to Java introduction course. Today, we are going to learn about modifiers in Java. Access modifiers in Java helps to restrict the scope of a class, constructor, variable, method, or data member. There are four types of access modifiers available in Java default, private, protected, and public. These access modifiers are used for encapsulation starting with default .A class, method or data member is said to be having the default access modifier. By default, it is when no access modifier is specified. The default modifier provides accessibility within the same package only. So let's take an example of the cat class in package p one. Here, the cat class has the default access modifier and the cat class is visible to all the classes that belong to the package p one. However, if we try to use the cat class in another class outside of package pone, we will get a compilation error. This is because default modifier provides accessibility within same package only. Next, we have private. The private access modifier is specified using the keyword private. The methods or data members declared as private are accessible only within the class in which they are declared. You can consider a password in this case. You don't want to share it with anyone, right? Similarly, the private modifier makes methods and members accessible within the class itself only. Even another class of the same package will not be able to access these members. So, let's take an example of a data class in package p one. Here we have declared a private variable named name in the data class. Next, we have a main class in the same package p one. Here we try to access the private member name of data class. When we run the program, we will get the following error the error is generated because we are trying to access the private variable of the data class from the main class, while the private member is only accessible within the same class. Next, we have protected. The protected access modifier is specified using the keyword protected methods and data members declared as protected could be accessed anywhere within the same package as well as from sub classes. You can think of a swing in the lawn of your house. The kids in your house could play with it as well as the kids and the neighbor. But since the swing is protected, it is not possible for anyone unknown to enter the house and play with the swing. Similarly, if we take an example of Java code here we have two packages p one and p two. We have a class in package p one and B class in package p two. To make a class accessible, it in package p two. We make it public. We add the method display in a class and make it protected. Next, we make B class extend from a class and access the protected member display of B class. This code compiles successfully and prints the output. This is a class. This is because the protected member display is accessible within the subclass B of A class. Lastly, we have public. The public access modifier is specified using the keyword public. The public access modifier has the widest scope among all other access modifiers. Classes, methods or data members that are declared as public are accessible from everywhere in the program. There is no restriction on the scope of public data members. You can take an example of your social media account, let's say your Facebook account. All your posts that you make public are viewed and accessed by anyone using Facebook. Similarly, any class, method or data member that you declare as public is accessible anywhere within program. Let's take this Java code as an example. Here we have a public class A in package p one with a public method display. Next we have another package p two. With B class. We import the package p one. And now, even without inheriting a class, we can access the public method display of class A in class B. This code compiles without any error and gives the output this is A class. This is only possible when you make the display method public. So we are done with all four access modifiers in Java. This was all from my side. I hope it was helpful. Thank you for watching.

access modifiers in java
java access modifiers
access modifiers
access specifiers in java
java access modifiers tutorial
access modifiers java
default access modifier in java
non access modifiers in java
access modifier
modifiers in java
java access modifier
access modifiers tutorial
access modifiers in oops
access modifier in java
access modifiers in java with example
public access modifier in java
private access modifier in java
Рекомендации по теме
visit shbcf.ru