Access Modifiers

Contents

Object-Oriented Programming
1
Overview
PREVIEW36s
2
What is OOP?
PREVIEW3m 44s

The course is part of this learning path

Start course
Difficulty
Beginner
Duration
1h 27m
Students
38
Ratings
5/5
starstarstarstarstar
Description

In this course, we will learn the concepts of microservice and spring framework with a focus on object-oriented programming.

Learning Objectives

  • Object-oriented programming in Java

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud

Prerequisites

  • Basic Java knowledge
Transcript

Hello there, my friends. In this video, we will learn about Access Modifiers in Java. Access modifiers are used for setting the access levels to classes, variables, methods, and constructors. There are four access modifiers in Java. The first one is default. It means we did not explicitly declare an access modifier for a class, field, method, or 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. The 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.

So, 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, other classes in the same package, subclasses, and classes in other packages. If you use 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. So, now let's look at the restrictive level of accessibility of modifiers. On this slide you can see the restrictive level of accessibility of modifiers. As you see in the slide, the most restrictive modifier is private and the least restrictive modifier is public. Default comes after private and protected comes after default.

So, 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 different packages in this project. I right-click on the source folder. I select the new class option. Let's set the package name first. Let this package be called my access package. 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 my access package and select the '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. Now, let's create a different package. I right-click on the source folder, I select 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 will 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 will create instance 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 is static we can already access it from different places. We'll talk about the static keyword in the next lessons. So, let's just keep going. Let this method print the following message to the console, "This is the public modifier". Now, let's create a second method with the protected modifier, protected void myProtectedMethod(). I will 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 will 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 will copy and paste the println() method here.

This should give the message, "This is the private modifier". 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 will create an object from the Car class in the main() method. Thanks to this object, we will be able to access instance 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 open the Bike class.

First of all, I will 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, myCar.private(). So, 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. So, we can only use a private() method or variable from within the same class and 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 open the Plane class. First of all, I will 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 will be able to access other methods? Let's see, 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 methods 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. So, we learnt the effect of access modifiers in practice. Great. We learnt 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? I'm going to answer that question really quick and end this lesson. You don't have to use access modifiers while creating any object 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've 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 on our projects. But as I just mentioned, it's okay if you don't use them. So, now we've covered the subject of access modifiers. Let's take a short break. I'll see you in the next video my friend.

 

About the Author
Students
1963
Courses
64
Learning Paths
4

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.