Java Fundamentals

Objects & Encapsulation

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

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

Angie demonstrates using classes to define the makeup of an object. A discussion regarding one of Java's core principles, encapsulation, is also provided in this segment.


Transcript from the "Objects & Encapsulation" Lesson

>> Okay so Java is an object oriented programming language. And objects are structures which contain data in the form of fields also known as variables, as well as behavior in the form of methods, so fields and methods, data behavior. All right, we use classes to define the makeup of an object.

So we're gonna create our first, well, not our first class, but we're gonna create our first class that represents an object that's gonna be used as a template. All right, so this is going to describe the fields in the methods of a rectangle. So we're gonna go to the IDE, and let's create a new package called objects.

And we'll create a new class inside of here called rectangle. Okay, so if we were to think about some fields that a rectangle would have, some characteristics, we might say something like a length. We could have a width. And then our rectangle has sides, right? So we could even declare that it's four sides.

All right, so these would be the fields for a rectangle. If we wanted to define behaviors, then we could do something like calculate the perimeter. So we could say double calculate perimeter, okay? And perimeter is what? Two times the length, 2 times length plus 2 times width So this is now a behavior, right?

You can calculate the perimeter or maybe we can calculate the area. And that one could be just the length times the width. All right, so this is a class with both fields and behaviors. We could do things like create nice little objects based on this class. Now notice here we don't have anything like, any access modifiers on the fields or the methods, meaning private words.

I wanna talk about encapsulation a bit. So Java has four core principles for object oriented programming. Encapsulation is one of them, what encapsulation prescribes is that all of the data in the behavior is contained within the object itself. So given an object, I can do what I need to do with that object.

And it says to expose the behavior, so that would be the methods and restrict direct access to the data. So that would be the fields. So we don't really want these fields just left like this without an access modifier because then they're protected. People can directly access these fields.

What we wanna do is, add a private access modifier in front of all of the fields. And then for the methods, this is the behavior we do want to expose the methods, right unless there was a good reason not to, right? So if you had a method in a class, that it was only specific to this class is some sort of process method that you don't want people to directly call.

You could put a private there, if it's something that you're making so that people can call into it and it's useful for them, you will make that public. So, we'll make our behaviors public and our data, our fields private, and we've just encapsulated this class. All right, so that's what encapsulation means in a nutshell.

>> I have a question about private.
>> Yeah.
>> In the past, I've read about stuff like people doing, bad actors doing like field ejection or to manipulate stuff that's going on in a program in a way that they shouldn't. Would this private keyword be used like for protecting stuff like that as an example or is that different?

>> It's different so, what you're describing is more like the input.
>> So when we accept that input, we would wanna make sure that it was clean. And that they haven't put any sort of sequel injection or anything like that in there. This is for example, you wanna control how people utilize the data.

So you would provide the methods, the behavior that allows them to do it. But you wouldn't want people to directly change the value of in objects field, for example. So you allow this with what's called getter and setter methods also call accessor methods, mutator methods. So we need to add those to this class cause right now people have no way to determine, what is length or set the width in some kind of way.

So we wanna provide them ways of doing that without them directly saying rectangle.length equals whatever, okay? So to do that, we wanna add getter and setter methods for all of the fields that you want people to be able to get information on or set in some way. So to do that, I'm going to do a little trick.

If you're in IntelliJ, you can right click, because it's a lot of boilerplate code, and if you go to generate. Select the fourth option, getter and setter, and then you can select the fields that you want getter and setter methods generated for. And then notice they've created them for me.

So the getLength is just gonna return the length, setLength will accept the length, and then, set it. Notice this keyword, is on line 22, we had this.length equals length. What does that mean? Remember, variables are local and scope, right? So, we have length here as the perameter name, but then we also have a global variable called length.

So what this is saying is set the global variable, the length that belongs to this class, set that variable equal to my local variable length. That's what that means, all right? Same for width and same for sides. And then that really gives us that encapsulated object.
>> So you said that this is the global?

>> Yes, this, so that means the variable that belongs to this class. So this will give you the global instance, and then this will be the local one.

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