‫Welcome back.
‫In this video, we're going to learn about access modifiers in Java.
‫Access modifiers are keywords used to control the visibility and accessibility of classes, methods
‫and fields within a Java program.
‫There are four main access modifiers in Java.
‫Public, private, protected and default.
‫But before we start learning about them, I'll move this class to a new file in order to understand
‫these access modifiers.
‫So I'll cut it and create a new Java class and I'll name it as car.
‫In this way we can create a class.
‫You can see here inside this package, This is the package, the icon of the package called Campmaster
‫Coding Lab.
‫I have two classes, car and my class.
‫I'll paste the car class here.
‫I'll start with the public access modifier.
‫Public access modifier.
‫When a class method or field is marked as public, it can be accessed from any other class or package
‫in the Java application.
‫So, for example, if I mark public and the class, you can see that it is marked by public.
‫So if I go to my class here, I can create an object from this car.
‫So here, car, red car, equal new car.
‫I am creating an object from it, so I am accessing it.
‫Okay, so it's visible to me.
‫Let's try to create a package, a different package, and see how we can access the public classes in
‫the different package in order to create another package.
‫Here we have the package.
‫Com dot master coding lab.
‫I'll go to Java folder, create new package, and here I'll name the package as test another pack.
‫And inside this another pack I'll create a new Java class named as test.
‫Till now I have two packages.
‫Com dot master coding, dot lib and another package and I have three classes two in the com dot master
‫coding dot lib and the test is placed inside another pack package.
‫I'll try to access the car.
‫So inside here I'll create a void.
‫Just testing.
‫This is the method.
‫I'll create a car.
‫Car, yellow car.
‫Equal to new car.
‫And I'll type.
‫70 with the 2024.
‫Please, guys, pay attention here.
‫You can see I can access the car because it's marked as public and car is placed in different package.
‫And in order to access it, I need to import the package called campmaster coding dot lib.
‫In order to import it.
‫Just hover here and click Alt+.
‫Enter import class and you can see I have imported the package before and then I imported the class.
‫This is how we can create or access the classes in the public access modifiers with the different package.
‫Let's move to the second access modifier Private.
‫When a class method or field is marked as private, it can only be accessed within the same class.
‫It's not visible to any other class or package.
‫If I go to the class car and mark the year here as private, if I go to test for example, or I can
‫go to the same package first my class, then we'll move to the test red car dot and you can see I can
‫access a brake speed and accelerate.
‫Only the year attribute is disappearing because we marked it as private so it's not accessible by the
‫class.
‫Even in the same package.
‫If I go to the test and try with the yellow car, of course I can't access the year protected.
‫When a class method or field is marked as protected, it can be accessed by classes within the same
‫package and also by subclasses.
‫So for example, let's go to the car and mark the speed by protected.
‫I'll go to the my class and try to access red car dot and you can see I can access speed.
‫You can see I am accessing speed, okay.
‫And I can set it as nine.
‫But if I go to the test class, which is in the different package and try to access the speed, I cannot
‫access speed equal to, for example, eight because speed is protected in the car class.
‫So protected you can access it by classes within the same package.
‫And the last type of access modifiers is default or package private.
‫If no access modifier is specified, it's considered as default access level.
‫A class method or field with default access can be accessed only by classes within the same package.
‫So for example, here we have accelerate.
‫I didn't specify here private or public or protected.
‫So let's try to access this accelerate method.
‫Let's start with my class, which is in the same package here.
‫I'll access red car dot accelerate.
‫I can accelerate if I go to the test class and try to access accelerate, I have an error because I
‫didn't mark it as public.
‫It's default.
‫So I can't access it from different package.
‫Remember that access modifiers play a crucial role in encapsulation and defining the visibility of various
‫components of your Java program.
‫By using the appropriate access modifiers, you can control the access levels and protect sensitive
‫data and methods from unauthorized access.