Java Fundamentals

Limiting Access & Access Modifiers

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

The "Limiting Access & Access Modifiers" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie discusses limited member access with inheritance, such as public, protected, and final members being inherited, but final methods cannot be overridden. When overriding an inherited method, the access modifier must be the same or less strict than the superclass.


Transcript from the "Limiting Access & Access Modifiers" Lesson

>> When a subclass inherits from a superclass not everything is inherited. As we saw, those private fields were not inherited. But there's a few other things that are not inherited as well, such as constructors. Constructors are not inherited technically, they are not considered as members of the class.

The members of the class are the fields in the methods, all right? So they are not inherited. All of the public and the protected members so the public and protected fields, public and protected methods those are indeed inherited. But the private members are not, okay. Also, if there are any final methods or any final fields meaning they had that word final in front of them.

They cannot be changed or anything like that. These are inherited but they cannot be overridden. That makes sense, right? You said this is the final implementation for this. In the square class here, we were able to use the fields size and length. Remember, we did that in our code and that's because we inherited those from the Rectangle class.

And the reason that we were able to inherit them is because we had to change it, right? To protect it in order to do so. If they're marked as protected, public or if there's no access modifier at all it defaults to protected. And so we will be able to inherit them.

If any of those were private then square would not inherit it. If it tried to use it as we saw, you get a compilation error.
>> So if you want to explicitly expose one of these methods for a class you have to mark it public. If you're not

>> Are protected. Yes. What's the difference between public and protected?
>> Package access.
>> Package access or not even so that's true but also with inheritance too. So even if I'm in a different package, so for example rectangle is in our objects package and square is in our inheritance package.

But those fields are protected so we're able to... So basically, if you're in the same package or if you're a subclass then you'll have access to protected stuff. If you're not a subclass and you're not in the same package, you do not have access to protected stuff. Public everyone has access.

Private no one has access but that class, all right. Okay, so let's talk about the access modifiers a little bit more. Notice for squares calculate perimeter method. It has a public access modifier. So when we overrode this method, we had to declare it as public as well. If we were to try to change it to anything stricter, we would get an error.

So if I'm overriding a method it has to be the same or less strict. It cannot be more strict, all right. Now it doesn't mean that we can't change it right. Like I said, it could be something that is less strict. So for example, a rectangle here if we had this method be protected.

And then in square, we overrode it and change it to public then that's fine. That's fine to do.
>> Is there any particular reason Java was set up that way? Or you can't make it l more strict, I guess?
>> So you're inheriting something but then you're changing like who has access to it.

I don't know. It just feels wrong [LAUGH] feels like a wrong thing to do. I don't know like the exact reason. But I know it's limiting access where that wasn't what was intended from the super class.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now