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 are honed, refined and improved over years. Then, that same part is reused repeatedly, as well as occasionally improved further. Now, if you are going to be picky about my analogy, then you can argue that each of the car's components still must be built from raw materials using real-life engineers, or robots, and so on. This is true. Just stick with my analogy a bit longer.
The important thing about OOP, Classes, and Objects
What the software engineers do when they write their code is to 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 (manufactures) our custom-built creations into working code that can be run by the Android device.
Note
We learned in chapter 1 that this working code is called DEX code.
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.
We are almost done with OOP- until later.
Final word on OOP, Classes, and Objects – for now
Analogies are useful only to a certain point. It would be more useful if we can summarize what we need to know at this point:
- Java is a language that allows us to write code once that can be used over again.
- This is very useful because it saves us time and allows us to use other people's code to perform tasks we might otherwise not have the time or knowledge to write for ourselves.
- Most of the time, we do not even need to see this "other people's" code or even know how it does its work!
One last analogy. We just need to know how to use that code, just as we only need to learn to drive the car- not manufacture it.
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 writing location aware apps and games. One of the things he does is he makes features such as getting the device's location in the world in a simple one-line task. So, the one line of code we saw previously sets many more lines of code in action that we don't see. This is an example of using somebody else's code to make our code infinitely simpler.
Note
Demystifying the satellite code
Here it is again:
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
The locationManager
is an object built from a class, getLastKnownLocation
is a method defined in that class. Both the class that locationManager
was built from and the code within the getLastKnownLocation
method is exceptionally complex- but we just 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.
Note
All methods are part of a class. You need an object built from the class to use the methods. All will be explained in Chapter 8, Object-Oriented Programming
Tip
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" even more than using classes. They use pre-made game libraries like LibGDX or complete game engines like Unity or Unreal. We will be learning Java without these cheats, leaving you well prepared to move on to libraries and engines should you wish to.
But where are all these classes? Do you remember this code from when we were typing the method definitions?
/* 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 that the previous code had an error. The first reason there was an error in the code MotionEvent
is that Android Studio doesn't know anything about the MotionEvent
class- yet. Note also in the previous code I have added a line of code.
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 previous code. Don't miss the semi-colon ;
on the end.
We will solve the MotionEvent
error when we discuss packages next.