Sunday, July 7, 2013

"Optional" Implementation on Interface Methods

Some interface definitions state that certain methods are optional. Iterator<E> is an example in Oracle Docs. The method remove() is said optional.

But Java language requires that every method in an interface is implemented by every implementation of that interface. There are no exceptions to this rule. It confused me for this point and I did some research. It turned out that the implementation is not really optional but it can be empty.

Take the remove() method of interface Iterator<E> as en example. The signature is
void remove();

To make this implementation optional, one can do one of following two. Either way, you are implementing the method.
void remove() {
    // empty
}
void remove() {
    throw new UnsupportedOperationException();
}
This is the real meaning of "optional" implementation. Of course, one can choose to actually implement the method like below.
void remove() {
    // do some thing here
    // validate state and remove element
}

Saturday, July 6, 2013

Interface Implementation and Class Inheritance

Short note:
If superclass implements some interface and is extended by some subclass, by default the subclass implicitly implemented all methods declared in the interface. Programmers do not need to explicitly specify the implements as in superclass. However, it won't hurt if they do so.

Interface definition:

Superclass definition:

Subclass definition can be either of following two:
(Programmers do not have to override the methods, even explicitly using implements)

Inheritance and Override in Java

Some learning and testing code on Java class inheritance and member override.

Super class in pkg1:

Sub class in pkg2:

Main class for testing in pkg3:

Output:
in super private method1
in sub public method2
in super private method3
in super public method4
in super package-private method5
in sub private method1
in sub public method2
in sub public method3
in super public method4

Some points about inheritance and override in Java:
  1. Only public or protected members can be overridden. In above test code, Line 21 in SubClass.java will break if commented out.
  2. If moving SubClass and SuperClass to the same package, Line 21 in SubClass.java will work since default access scope is package-private.
  3. private or package-private members cannot be overridden. Even they have definitions with same signature, they are different methods and overriding rules and resolution don't apply to them.
  4. If calling in subclass, even with super, JVM will try to locate overriding members in subclass. Again, this happens only when the member is declared as public or protected.

Thursday, July 4, 2013

Android App Development Notes


  1. Error: "string_id" is not translated in es... (languages)

    Eclipse > Window > Preferences > Android > Lint Error Checking > Change the Severity for "MissingTranslation" to Warning

  2. Error: Attribute is missing the Android namespace prefix

    Add prefix "android:" to the element that complains, like "android:color="#A00080FF"

  3. Class requires API level 11 (current min is 8): android.view.ActionMode.Callback

    One solution is to change android:minSdkVersion from "8" to "11" in Manifest.xml.


How to Implement Singleton in Java

There are multiple solutions for this. Let me describe one by one.

Solution 1:

Solution 2:
Compared to Solution 1, this one is easier to change the singularity if things change.

Solution 3:
Compared to Solutions 1 and 2, this one is lazy instantiation and used double checked locking. Do not synchronize the method instead because for most cases if the instance is already initialized, threads want to return right away. Also, note the volatile in Line 2. This is used to avoid the out-of-order issue of double checked locking solution.

Solution 4:
This is a preferred solution as of Java v5.0.