OOP: Modifier Types

Learn the Basic Java Concepts Object Oriented Programming
17 minutes
Share the link to this page
Copied
  Completed

Transcript

Hello there, and welcome back to this Java development course. So last time we went over inheritance and got some homework to do, your job was to create two classes an insect class with an int, number of legs attribute and a spider class with an int number of eyes attribute and then create constructors for both of them. So here is essentially what you should have done. So first of all, let's take a look at my insect class and then the spider class had to extend the insect class I forgot to mention that I mentioned it previously, but this time, anyway, the insect class, so we have an int number of legs, then we have a public insect. We take in an int number of legs as input. Then we assign the input corresponding class level variable, number of legs.

Now we also have a spider class which extends insects Which has a number of eyes attribute which is an integer. And then we have a constructor here which takes in a number of legs and a number of eyes as input, then passes in the number of legs to the class that extends insect and then assigns the number of eyes to the corresponding class level variable, number of eyes right here. Alright, so that is essentially what should have done. Now let's test this out. So we're going to go ahead and delete this. And then we're going to have a spider.

Spider r equals new spider. Alright, and now let's input number of legs first. It was gonna have eight legs. Do spiders have eight legs and then eight guys. Let's just keep it at bay. Okay.

And then we'll just do system dot out dot out dot print ln. All right, there we go. And so now we're going to print our DOT number of eyes. Let's do the same thing for legs as well. So our dots number six. All right, there we go.

So now let's run this. And there we go. There we can see it prints eight, and eight, since that is what we assigned in the constructor. So there we go, that essentially, we should have done in this framework. So today, let's go over a very important part of Java, we're going to be going over modifier types. So modifier types are essentially a whole nother story altogether.

So let's go over exactly what they are. Alright, so there are a couple basic modifier types that we're going to go over. So let me go and write them out here. So we have private Okay, then we have public then we have default and then we have protected All right, and those are the basic modifier types. Now I did I do things a little bit wrong instead of let's just keep it like this since this way There we go. So essentially what I'm what I just did here is ranked them from most private to most public circles private and protected than default and public, you'll see exactly what I mean in a second here.

So let's just say that we have this spider class right, so we have a spider class and now let's just say we put in a private modifier here before in so we do private int number of eyes. You'll see now that we have an error here. So when we do our DOT number of eyes, it says the field spider DOT number of eyes is not visible. This is because the protected modifier only actually makes an element of a class visible within In the given class, so if we have a private int number of eyes, we can only use this inside this class spider outside of the class spider, we cannot use this. And the same applies for methods as well. So if we do private spider here, you'll see that we get an error here as well, since it is now a private method, the constructor is no private, and so we cannot actually use it outside of the class outside of the Spyder class.

Alright, so that is the private modifier. Now, let's take a look at the protected modifier. Alright, so looking back actually made a mistake, not the protected modifier here, it's supposed to be the default and then protected. So protected is more public than default. There we go. All right.

Now the default modifier okay. So the default modifier is what essentially what we've been doing right now. So if we remove the private, any modifier, essentially any access modifier, and in this case, we now have our variables set to the default modifiers. The default modifier essentially just means that this variable or method can be accessed anywhere in the package. So let's go back to our project structure here. Let's take a look.

You'll see how our classes and objects file as where as well as our spider file are both in the same exact package object oriented programming, right? Well, since they are both default modifiers we can use them within this package, but if we try to use them outside of this package, let's just take a look. Try an RF medic operators we're going to try to create we're going to try to take spider we're going to create a spider spider s equals new spider. There we go. And let's import it. First of all, we have to import it since it is in a different package.

I will go Over importing soon and then also we have to add some arguments here. And there we go. And now you'll see we actually get an error the constructor spider int is not visible. So since inside spider, this constructor is default, it means that we can only use this constructor in these in the package that spider is in. So in the package object oriented programming, since we are trying to use this constructor outside of that package, in our in the project one package, it in result gives us an error that we cannot see spider. So now we've sort of, you know, changed put ourselves into a trap.

So we can't actually create a spider variable, since we have to have a constructor for it, but we can access the constructor that we have for it, since it is default and so we can't see it. It's not visible to us in this package. So if we remove this, then it will still give us an error and so That is essentially the default modifier. Now the default modifier is since it does make an attribute or a method only acceptable within the same package or only visible in the same exact package, that is that the that the classes in in which the attribute is in, in results. It also has a name package, but essentially also called default modifier. Since that is what is the default if you don't assign any modifier, all right now, there is also the package modifier, non protected modifier the pecs modifier we just went over so, the protected modifier essentially works a lot like the default modifier in that if there is a protected attribute in a class, that attribute will only be visible inside that package.

But there is a little bit of a difference. So if we have another class outside the people package, have the class with the protected attribute. But that class outside the package extends the class that is that has the protected attribute, then it can actually use that attribute and access it, it is visible. But if it doesn't, then it isn't. So if it's not, if it doesn't inherit it, then it isn't. So let's take a look at an example right now.

So we can go ahead and open up spider dot java, and whoops, there we go. And you'll see how now we have a default in number of eyes. Now if you go to automatic operators, and let's just create a new spider. So and now you'll see that arithmetic operators here is in a different package than spider here. So since it is a default modifier at this point, it shouldn't be able to see the constructor so Let's go and do spider are equals new spider zero comma zero. And as you can see, it can't see the constructor since the constructor spider is not visible.

Now let's go ahead and add in an attribute to spider we're going to add in protected. Actually, first of all, it's going to actually do it a little bit differently just that you can see the example we can now do our thematic operators extends spider. There we go. Now, we need to add a constructor for it, obviously. And there we go. Now you'll see here now that the constructor spider is not visible.

So we are extending this class, but we are not actually we can't see the constructor. So the constructor here we can't see it. But if we are protected here, then in that case, there we go. We can see See it. As a result, we can now actually see this. So essentially, that is the way that works.

So we can use the super keyword to see the constructor. Otherwise we can't. So you'll see here actually, if we try to access this constructor without actually using the super keyword, then we can't, since we asked to be public, in order to be seen in different package, but if we use the super keyword we can and Same goes for number of eyes. So if you do private protected, pro protected into number of eyes. Here, we can now do super dot. Oops.

Let's do system. dot out dot print ln. Super dot. Actually, yeah, we can't actually put it in here. There we go. Okay, let's try right here.

Super DOT number of eyes as you can see, but if we remove this protected model And just keep it at the default modifier. It is now only visible in this package and it doesn't matter if fixed extends it or not and so in result if we do super.it does actually give us any number of eyes attributes which finally leads us into our final modifier the public modifier and the public modifier essentially just the most open modifier that there is all right so the public modifier if we go and actually make everything public inside Spyder so we'll do public here and public constructor as well. What seems alright I replaced it with public There we go. Public int number of eyes. And now if we go to our symmetric operator, we can now as you can see, not extend spider. All right, and we can now access spider dot whoops no spider r equals new spider and then zero comma zero There we go.

So as you can see, even though spider is in a different package, we can still access the spider constructor since it is public. So that essentially is sort of the, the most open constructor. So if you want everybody to see it, then you use public. And if you want to see it in another project, you can't just doesn't work that way. I mean, there are ways to get around it, but essentially just combining them together. So yeah, so public is the most public keyword that you have.

Okay, and now, one last keyword that we're going to go over is the static keyword. Let's go over that as well. All right, so now in particular, our main method so now we know that this is the method we went over that we know what void is void essentially just means that our method doesn't return anything. Then public we now know what that is, as well. Whoops, public means that it is visible even outside the package. So everywhere, essentially, but we now we don't know what static is.

What is this static and why is it here? Well static essentially means that we can refer to a method or a variable without actually creating an instance of that class. So let's create two variables here. So we're going to have an int a equals one, okay? And then it and then static. Int b equals two.

Let's actually put this in spider inside the spider class, let's give them some better names as well. So we're going to have whoops, there we go. So the age is going to be without static and then the B what is what would this be? So age is one and generational see generation. I don't know what what what generation is blushes say generation, okay, so in age equals one and then static in generation equals two now If we go to spider here, we can now let's delete this right here. So we can now if we do spider and notice how we're not actually doing new spider we just do spider dot generation you'll see we can actually refer generation knows we actually put this in a print statement.

So we'll do System dot out dot print ln, whoops, nope, print ln spider dot generation. There we go. And now if we run this, we get to so you'll see how since inside spider we have a static in generation. We don't have to do new spider in order to get this attribute but if we wanted to get to the attribute age, which is one and not static, we would have to do new spider so we'd have to do new spider dot. All right, we have to put a parenthesis we have to instantiate call the constructor zero comma zero And then only after that can we do dot h as you can see. So now we can still refer to generation since it is static we can still refer to it.

But we can also refer to age if we do new spider and then dot age when we do new spider and then dot essentially doing the same thing as creating a spider and assigning into something and then actually referring to it directly so this is the same thing as just doing spider are equals new spider and then doing our dot age is the same exact thing essentially. All right, and so that is the static modifier. Now I said that the static modifier was the last one we're going to go over but there's one last modifier, it's the final modifier. That was confusing. Anyway, the final modifier it's it is the last modified that we're going to go over in this lesson. So it's final.

And then it's also called the final modifiers. The final modifier essentially means that we create a constant instead of a variable. So if we have int a equals two, right? We can then do a Three, right, so we have changed its value. But if we add final here, final a final int a equals two, then we get an error. Since a local variable a cannot be assigned, it must be blank and not using a compound assignment.

So as you can see here, since a is final, it's now a constant, and so it can't be changed. That's essentially all a final is and it can be used on any day, any data type. So even on complex data types, like strings, or classes, and so on. Alright, there we go. So that is essentially how we work with modifier types in Java. Alright, so homework today is pretty simple.

Just create a class that has one of every single datatype. So you're going to have a public attribute, a private attribute, a protected attribute, a default attribute, a static attribute, and a final attribute as well. All right, so that essentially your homework Very simple just, you know, just create them and understand what they mean. That's also an important part and yeah, so without further ado, I wish you luck and I'll see you next time soon.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.