Viewers: in countries Watching now:
Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.
The principles of object-oriented programming depend on a few basic concepts and one of the most important concepts is encapsulation. Encapsulation means that you're packaging complex functionality to make it easy to use. When you first create Java applications, most new programmers will put all of their code into the main method, and I've been doing that so far in this course. But you quickly find out as your applications get larger and gain features that putting all the code into one place makes that code very tough to manage.
So instead, you want to break the code out into individual classes, grouping the functionality, however, the logic dictates for your application. When you do this, you have some big advantages. One advantage is that you can restrict access, one part of the application might be able to use a particular function or another part of the application wouldn't be allowed. If you put all the code in one place, that's tough to restrict. When you break it apart, it's easier. Similarly, when you store data in a Java class, if you wrap it up as encapsulated data, you have the option of hiding how it's being stored.
You don't have to tell the user of the data, that is a programmer who's calling a particular class, whether that data is being stored as an array, a collection of some kind, or any other object. You simply provide methods that the user of that class can call and all the complexity is hidden. One way to think about encapsulation is to look at an analogy of the 3D world. Let's take an example of an industrial machine, in this case, an olive press. In order to use an olive press, if you were asked to know everything about how the press worked, it would be very difficult to get started.
If you had to know which direction each gear was going to turn, which order each of the pressing arms is going to come down, and how much oil might come out of each individual olive, it would be very difficult to get any work done at all. So most real industrial machines are encapsulated and that means that they wrap up their complexity in a big box and then you can simply add the olives in the case of an olive press, perhaps press a button and out comes the olive oil. It's much easier for a new user to understand how to use the olive press.
There is still complexity, there are still gears, there are still pressing arms, there are still calculations to be done, but the whole process of getting started with the machine is greatly simplified. Code-encapsulation works the same way. Let's take an example of non-encapsulated code. Again, if you're a new Java developer, you might be tempted to put all the code for the application in the main method of your primary class. In this example, I'm creating an array and then I'm creating instances of two other classes, and then I'm looping through the array and doing something with it.
This little bit of code might not look too daunting but imagine if the code had expanded so you had literally hundreds or thousands of lines of code all in one place. Then somebody comes along and says would you add a new feature to the application or I found a bug and it needs to be fixed. This sort of monolithic code is very, very difficult to manage. So in Java applications, you're encouraged from the beginning to break your applications down into small pieces. So for example, I may take some of that code and move it off into its own custom class.
I might create a class called OlivePress. Within the OlivePress class, I might declare a private field or a private variable. In this case, it's an array of olive objects named olives, and then I might have a constructor method that allows me to pass that data in as I create an instance of the class. Then I might have a method called getOil that can be called from anywhere in the application. This method would encapsulate the complexity of how you have to loop through the list of olives, crush them and add them to the olive oil.
The developer of this class needs to understand and deal with the complexity, but the user of the class only has to call the getOil method and so the primary code of the application can now be very brief and very easy to read. I would create an instance of the OlivePress, I'd pass in my olives, I'd call the getOil method and I get back the oil. All of the mechanics and the complexity of how the operation happened would be hidden from me. All I need to know is call the method, get the result.
So that's how encapsulation works. The benefits of encapsulation include that you can break your functionality down into small maintainable units. It's much easier to fix bugs when you're looking at a small amount of code and much more difficult when you're looking at a massive amount of code. So encapsulation helps you maintain the application over the long-term. You can also use encapsulation to group functionality and data together. In Java, as with most object-oriented languages, you can define classes that group the data and the functionality that manages that data together or you can create individual classes; one for the data, one for the functionality, and bind them together using a process called loose coupling.
And finally, all of this together means that you can test your software and maintain it over the long- term at a granular level. Rather than looking at your application as one huge block of code, you're breaking it down into small manageable pieces. So in the rest of this chapter, I'm going to talk about the mechanics of this in Java. How do you create your own custom classes? How do you move code from a monolithic application and break it down into smaller pieces? And how do you declare classes that serve as utility libraries or that are their own custom classes that can be instantiated and seen as data objects all on their own?
Find answers to the most frequently asked questions about Java Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.