A (175)

(Tuis.) #1
CHAPTER 5: Introduction to Java: Objects, Methods, Classes, and Interfaces 155

the java.lang.System and java.lang.String classes are declared to be final so that their functionality
cannot be altered.


All methods in a final class are implicitly final. Any method that is declared using the final modifier
keyword cannot be overridden by subclasses. This is also for security reasons and is used to
prevent unexpected behavior from a subclass altering a method that might be crucial to the function
or consistency of a class’ functionality.


You can explicitly initialize a final variable only once. A reference variable that is declared as final can
never be reassigned to refer to a different object, if the variable references an object, rather than a
data value. If the final variable references an object, the data contained within that object can still be
changed, only the reference to the object is “fixed,” and is said to be final.


Thus, you can change the state of an object referenced by the final variable, but not the reference
to the object, which is what is “locked” or final. With variables, the final modifier is often utilized in
conjunction with the static modifier to make the class variable into what is considered a “constant”
or an immutable fixed variable for the duration of the class.


As an example, the constant named app_name that you defined using XML in the
strings.xml file would have to be declared in your application’s Java code by using the following
single line of Java syntax:


public static final String app_name = "Hello Universe";


This shows how using XML to define constants is much simpler than using Java. Next, let’s take a
look at the abstract modifier keyword, which allows you to create classes that can be subclassed
but not instantiated.


The abstract Keyword


A class declared using an abstract modifier keyword can never be instantiated, so it follows that
a class logically cannot be declared using both an abstract modifier and a final modifier, because
if a class cannot be instantiated or extended, it will not be of much use in the scheme of the Java
programming language.


If a class is declared as abstract, then the sole purpose for that class is to be extended—that is,
subclassed. If a class contains any methods that have been declared using the abstract modifier,
then the class should also be declared using the abstract modifier. If a class contains abstract
methods and is not declared as abstract, a compiler error will be “thrown” when you use the Eclipse
Run As ➤ Android Application work process, which invokes the Android Java code compiler. Every
time the compiler encounters an error, it “throws” it into the error log, which is now monitored in real-
time by your LogCat tab in Eclipse. It is important to note that if your application will not compile, it
also will not run!


An abstract class can, however, contain both abstract methods as well normal (non-abstract)
methods, so the rule is if you want to put an abstract method inside of a class, make that class
abstract as well, or you will get a compiler error. You cannot have abstract methods inside of a class
that is not also declared as abstract.

Free download pdf