The course is part of this learning path
In this course, we will learn the concepts of Java EE 7 with a focus on Java Basics.
Learning Objectives
- What Java classes, Executable Java, Access Modifiers are and how they work
Intended Audience
- Anyone looking to get Oracle Java Certification
- Those who want to improve Java 7 EE knowledge
- Java developers
Prerequisites
- Have at least 2 years of Java development experience
Hi there. In this video, we will learn Access Modifiers in Java. Access modifiers are used for setting the access level to classes, variables, methods, and constructors. There are four access modifiers in Java. The first one is default. It means we do not explicitly declare an access modifier for a class, field, method, and constructor. A variable method and constructor declared without any access control modifier is available to any other class in the same package. The second one is public. Public access modifier is accessible from everywhere. This is the least restrictive access modifier. The third one is private. The private access modifier is accessible only within the class. Methods, variables, and constructors that are declared private can only be accessed within the declared class itself. The private modifier is the most restrictive access level. The last one is protected. The protected access modifier is accessible only within the same package and in addition from all subclasses of its class. Now, let's examine these access modifiers through the table on the slide.
Notice that when you use the public modifier when creating a class or method or variable, we can access it from the same class or other classes in the same package, subclasses and classes in other packages. If you used the protected modifier, you can access it from the same class, different classes in the same package, and subclasses but not from different packages. If you don't use any access modifier, that is, if you leave it as default, you can still access it from the same class and from different classes in the same package, but not from subclasses and other packages. If you use the private modifier, this time you can only access it from the same class, not from other classes or packages. Now, let's look at the restrictive level of accessibility of modifiers. In this slide, you can see the restrictive level of accessibility of modifiers. As you see in this slide, the most restrictive modifier is private and the least restrictive modifier is public. Default comes after private and protected comes after default.
Now, let's move on to Eclipse and learn about access modifiers in practice. First, I will create a new project. I click on the 'File' menu in the upper left corner and select the 'New Project' options. Let's name our project Access Modifier. I create my project by clicking the 'Finish' button. Now, let's create two packages in this project. I right-click on 'src', source folder. I choose the 'New Class' option. Let's set the package name first. Let this package be called myaccesspackage. Let the class name be Car. This class will have a main method and I click the 'Finish' button. Now, let's create a second class in the same package. I right-click on 'myaccesspackage' and select 'New Class' option. Let the name of this class be Bike. I click the main method box and click the 'Finish' button to create my Bike class. Yes now, let's create a different package. I right-click on the 'src' folder. I choose the 'New Class' option. Let's set the package name first. Let this package be called otheraccesspackage.
Let the class name be Plane. This class will also have a main method and I click the 'Finish' button. As you can see, I currently have two different packages, and I have two separate classes in the first package, and I have a class in the other package. Now, I'll create some methods with different modifiers in the Car class and try to access these methods from all classes, respectively. Let's see which ones we can access from where. Let's open the Car class. I'll create instant methods within the class. Let's create our first method. I will create my first method with the public modifier, public void myPublicMethod(). This method will not be static because if it's static, we can already access it from different places. Let's continue now. Let this method print the following message to the console, "This is the public modifier." Yes. Now, let's create a second method with the protected modifier, protected void myProtectedMethod(). I'll copy and paste the println method here.
This should give the message, "This is the protected modifier." Now, let's create a method without using any access modifiers, void myDefaultMethod(). I'll copy and paste the println method here. This should give the message, "This is the default modifier." Finally, let's create a method with the private modifier,
private void myPrivateMethod(). I'll copy and paste the println method here. This should give the message, "This is the private modifier." Okay. We created our methods using all access modifiers. Now, let's try to access these methods first from this class, then from the Bike class, which is another class in this package, and finally from the Plane class in another package. First of all, I'll create an object from the Car class in the main method. Thanks to this object, we'll be able to access methods in the Car class. Car myCar = new Car(). Now, let's call our methods using the myCar object. I'm typing myCar. here. After putting the dot, we can see the methods we can use. If you notice, we see all four methods here.
Now, let's call these methods one by one, myCar.myPublicMethod(), myCar.myProtectedMethod(), myCar.myDefaultMethod(), and finally, myCar.myPrivateMethod(). As you can see, we can access all methods from within the same class even though we use different access modifiers for the methods. Now, let's run the application and test it. As you can see, the messages we created inside the methods appeared on the console screen. Now, let's try to access these methods from another class in the same package, namely the Bike class. I opened the Bike class. First of all, I'll create an object from the Car class in the main method. Car myCar = new Car(). Now, let's try to access the methods in the Car class in a similar way, myCar.myPublicMethod(), myCar.myProtectedMethod(), myCar.myDefaultMethod(). As you can see, we can access methods with the public, protected, and default modifiers. So, do you think we will be able to access private? Let's see now. myCar.Private(). As you can see, there is no method, so we couldn't access it.
Let's write the whole method. If you notice we get a compilation error. Therefore, we can access a method or variable with the private modifier only from within the same class, not from a different class. Let's run the application and test it. As you can see, the public, protected, and default messages are on the console screen. Now, finally, let's try to access methods from the Plane class in a different package. I opened the Plane class. First of all, I'll create an object from the Car class in the main method, Car myCar = new Car(). Now, let's try to access the methods in the Car class in a similar way, myCar.myPublicMethod(). We were able to access the public method. Do you think we'll be able to access other methods? Let's see now. myCar.myProtected(). As you can see, there is no method, so we couldn't access it. Let's write the whole method. If you notice, we get a compilation error. Let's write other methods. myCar.myDefaultMethod(), myCar.myPrivateMethod().
As you can see, we could only access the method we created with the public access modifier from a different package but not the other methods. Now, let's run the application. And there is only the public message in the console. Yes. So, we learned the effect of access modifiers in practice. So, do we have to use an access modifier or when should we use private and when should we use public? Let's give a short answer to this question and end the lesson. You don't have to use access modifiers while creating any objects or methods. This will not cause any errors. When you become a good developer in the future, while working with different developers on large projects, you may sometimes need to restrict the access of other developers to the objects you create. This prevents other software developers from using the objects that you have created by software and a more efficient working environment is created. In these cases, you may need access modifiers here. We generally use private or public access modifiers in our projects. But as I just mentioned, it's okay if you don't use them. I think the subject of access modifier is okay. Now, let's take a short break here. I'll see you in the next one.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.