[MUSIC] Welcome to the next lesson in the module on classes in Java. In this lesson, I introduce you to some examples, to motivate the purpose of classes and objects as powerful constructs in object oriented programming. After completing this program, you should understand the need for classes and objects and be able to distinguish between them, know how to apply the term abstraction to real world programming scenarios, and recognize the two major components of a class which are explained in the context of Java Strings. So let's start with this lesson. [MUSIC] We are able to model many things using simple primitive data types. Integers can represent counts a double the cost of something. Characters may represent the days of the week and Boolean values can tell us the state of some condition as true or false. But how could you use these primitive data types to model a student, a shape, a city, or a chess board? That doesn't seem too difficult take for example a city we could create string variables and doubles to represent the name, latitude, longitude, country and population. Five variables could represent a single city. But what if we need to represent several cities, or an array of cities? The variables begin piling up and are code becomes chaotic. We need a data type that can represent a city, all of the aspects of a city. We also need to consider the methods used with a city and keep them separate from methods that have nothing to do with our city. Let's take a look at object oriented programming for a solution object oriented programming can be thought of as an approach to problem solving. Java is a language specifically written to facilitate object oriented programming. An object in the context contents we are using here is the encapsulation of both data and behavior into a single container or object which can then be used to represent or model something in the real world. Object-oriented programming uses objects to perform most of its interactions. You'll soon see that this is very different from programs that use only primitive data types and methods to perform actions. These objects will allow us to model a much more complex world while still keeping this complexity manageable. A wonderful side effect of creating and using objects is something we call abstraction. And we've all encountered this many times. Take a car for example. Many of us would claim that we know how a car works. We can start a car, make it go forward, backwards, and stop. Some of you may even know how to change the oil in a car, or fix a flat tire but if something went wrong with the engine, if the car suddenly stopped working I dare say that most of us would be at a loss. This is where our knowledge of how a car works comes to an end. We are operating under abstraction here. We know enough about how a car works to make it work for us beyond that, many of us will rely on someone else to get the car going again. But this doesn't seem to stop us from owning and operating cars. It's the same with objects. If we can understand them enough to use them, then they have served their purpose. As an Android programmer, you will end up writing your own descriptions of objects that others will use without ever having to fully understand their implementation. We've already used the term class to refer to the file you create when you're writing a Java program. The term class can also be used to describe the template for an object such as a student, city, or chessboard. An object is then an instance of a class. Game3, for example, might be an instance of a chessboard. Let's review the string class. This is a class that's already included in the Java languages and enables you to use string objects without knowing all the details about how they work. The string class refers to the file that contains all of the implementation details of a string. A string object is an instance of a string such as the string apple. A string is a sequence of characters. The variable string1 points to or references the string object abcd. This is different from the primitive data type int. In the memory location referenced by the variable num, we will actually find the representation of the number 23 in binary. In the memory location referenced by the variable string 1, we find a reference to yet another location and memory where the string abcd is actually stored. But there are still some similarities between objects and primitive data types. For example, we can print a string in much the same way as we print an integer. String literals can be assigned to variables. And variables can be used to reference their contents. In the case of a string object we can even use the plus symbol. When it is applied to strings the plus symbol can concatenate the two operands to form a new string. But this is where the similarities end. Recall that one of the characteristics of an object is that both the data that represents that object and the method that is used on or by the object are grouped together. Once a string is assigned a variable, we can use string methods to interact with that value. We can concatenate another string with our string we can extract part of the string, and we can print the string contents. Let's take a closer look at how a method is called on an object. Notice the name of the object, in this case, a string, comes first, followed by a period or dot. Then we have the name of the method, followed by parameters, if they're necessary, for that method, otherwise, empty parenthesis will be use. We need to pay attention to what is being returned in each of these methods. For example, the concat method does not change the original sting it returns a new string. We need to either assign a return string to a variable or print out the results. Here are a few other commonly used String methods substring will return a new string. Starts with a boolean value of true or false. Length returns an integer. In each case we've assume how preserve the returning value. Comparing objects is also very different from comparing primitive data types. We should never use the double equals to compare the contents of two objects. The program will compile and run but you won't always get the results you expect. Instead we use a method specifically designed to compare objects called the equals method. Why is this the case? Well, to fully understand that we should build our own Class file and get a a good look of what's going on behind the scenes. [MUSIC] This concludes our introduction to classes in Java. In this lesson you began to build your object oriented vocabulary with these first few concepts; class, object, and abstraction. Now that you've completed this lesson you should understand the need for, and differences between classes and objects as well as to be able to distinguish between a variable that can be represented as a primitive data type such as an int, car, or Boolean versus a more advanced object that has both state and behavior such as Java strings. You should also know how to apply the term abstraction to real world programming scenarios and recognize the two major components of a class. By peeling back a layer of abstraction, we have seen how the already familiar string class works and how it differs from primitive data types. Before moving on I encourage you to try the self assessment quiz included with this lesson, to be sure you are building a firm foundation. We have a lot more to go in this module on classes. So let's continue with another lesson. [MUSIC]