In this course, we'll learn the object-oriented concept in Java.
Learning Objectives
- Object-Oriented programming concept
- Object & Class
- Access Modifiers
- Naming Conventions
- Constructors
- Packages
- Static Keyword
- Nested and Inner Classes
Intended Audience
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
Prerequisites
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
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 sub classes 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, sub-classes, 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 sub classes, 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 sub classes 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 'Source Folder.' I choose the new class option. Let's set the package name first. Let this package 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. We'll talk about the static keyword in the next lessons. 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 will copy and paste the println method here. This should give the same 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. 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, will be able to access instant 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 open 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.