Learning Java by Building Android  Games
上QQ阅读APP看书,第一时间看更新

Overriding methods

There is one more thing to know about methods before we do some more coding. All the methods I mentioned previously (draw, takeShot, newGame and printDebuggingText) are methods that we will be coding. They are our very own methods for our use only.

Some methods, however, are provided by the Android API and are there for our (and all Android programmers) convenience for us to either ignore or adapt. If we decide to adapt them it is called overriding.

There are lots of methods we can override in Android, but one method is overridden so often that it was automatically included in the auto-generated code. Have a look at this part of the code again:

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
}

In the previous code, we are adapting and overriding the onCreate method. You will probably also notice that the prefix and postfix to the name look quite complicated. What exactly is going on here will be explained when we more thoroughly deal with methods in Chapter 4, Structuring Code with Java Methods.

Note

The code super.onCreate… will be talked about in depth also but if you just can't wait for a brief explanation here is one. The super.onCreate… part of the code is calling another version of onCreate that also exists, even though we can't see it. This is the one we are overriding.

Now we can add the method definitions to the Sub' Hunter code.