Introducing OOP
OOP makes it easy to do exceptional things. A simple analogy could be drawn using a machine, perhaps a car. When you step on the accelerator, a whole bunch of things are happening under the hood. We don't need to understand what combustion or fuel pumps are because a smart engineer has provided an interface for us. In this case, a mechanical interface, that is, the accelerator pedal.
Take the following line of Java code as an example; it might look a little intimidating so early on in a book for beginners:
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
However, once you learn that this single line of code searches Space for available satellites, and then communicates with them in orbit around the Earth while retrieving your precise latitude and longitude on the planet, it is easy to begin to glimpse the power and depth of OOP. Even if that code does look a little bit long and scary, imagine talking to a satellite in some other way!
Java is a programming language that has been around a lot longer than Android. It is an object-oriented language. This means that it uses the concept of reusable programming objects. If this sounds like technical jargon, another analogy will help. Java enables us and others (such as the Android development team) to write Java code that can be structured based on real-world "things," and here is an important thing to note: it can be reused.
Classes and objects
So, using the car analogy, we could ask the question: if a manufacturer makes more than one car in a day, do they redesign each part before fitting it to each individual car?
The answer, of course, is no. They get highly skilled engineers to develop exactly the right parts that have been further honed, refined, and improved over a number of years. Then, that same part is reused repeatedly, as well as occasionally improved further. Now, if you want to be picky about my analogy, then you could argue that each of the car's components must still be built from raw materials using real-life engineers, or robots. This is true. Just stick with my analogy a bit longer.
The important thing about OOP
What software engineers do when they write their code is they build a blueprint for an object. We then create an object from their blueprint using Java code, and, once we have that object, we can configure it, use it, combine it with other objects, and more.
Furthermore, we can design our own blueprints and make objects from them as well. The compiler then translates (that is, manufactures) our custom-built creations into working code that can be run by the Android device.
Classes, objects, and instances
In Java, a blueprint is called a class. When a class is transformed into a real working thing, we call it an object or an instance of the class.
Tip
In programming, the words "instance" and "object" are virtually interchangeable. However, sometimes, one word seems more appropriate than the other. All you need to know at this point is that an object/instance is a working realization of a class/blueprint.
We are almost done with OOP – for now.
A final word on OOP, classes, and objects – for now
Analogies are useful only to a certain point. It would be more useful if we simply summarize what we need to know right now:
- Java is a language that allows us to write code once that can be used over and over again.
- This is very useful because it saves us time and allows us to use other people's code to perform tasks. Otherwise, we might not have the time or knowledge to write it for ourselves.
- Most of the time, we do not even need to see other people's code or even know how it works!
Let's consider one last analogy. We just need to know how to use that code, just as we only need to learn how to drive a car, not manufacture one.
So, a smart software engineer up at Google HQ writes a desperately complex Java program that can talk to satellites. He then considers how he can make this code easily available to all the Android programmers out there who are writing location-aware apps and games. One of the things he does is that he turns tasks, such as getting a device's location on the planet's surface, into simple one-line tasks. So, the one line of code we saw previously sets many more lines of code into action that we don't see. This is an example of using somebody else's code to make our code infinitely simpler.
Demystifying the satellite code
Here it is again:
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
locationManager
is an object built from a class, and getLastKnownLocation
is a method defined in that class. Both the class that the locationManager
object was built from and the code within the getLastKnownLocation
method are exceptionally complex. However, we only need to know how to use them, not code them ourselves.
In this book, we will use lots of Android API classes and their methods to make developing games easier. We will also make and use our own reusable classes.
Reusable classes
All methods are part of a class. You need an object built from a class in order to use methods. This will be explained in more detail in Chapter 8, Object-Oriented Programming.
If you are worried that using these classes is somehow cheating, then relax. This is what you are meant to do. In fact, many developers "cheat" much more than simply using classes. They use premade game libraries, such as libGDX, or complete game engines, such as Unity or Unreal. We will teach you Java without these cheats, leaving you well prepared to move on to libraries and engines should you wish to.
But where are all of these classes? Do you remember this code from when we were typing the method definitions? Take a closer look at the following code:
/* This part of the code will handle detecting that the player has tapped the screen */ @Override public boolean onTouchEvent(MotionEvent motionEvent) { return true; }
There are two reasons why the previous code had an error. The first reason is that Android Studio doesn't know anything about the MotionEvent
class – yet. Additionally, note that in the previous code, I have added a line of code, as follows:
return true;
This is the second reason there is an error. This will be fully explained in Chapter 4, Structuring Code with Java Methods. For now, just add the highlighted line of code, return true;
, exactly where it appears in the preceding code. Don't miss the semicolon (;
) at the end.
We will solve the MotionEvent
error when we discuss packages next.