Ben Chuanlong Du's Blog

It is never too late to learn.

Object Oriented Programming in Java

Tips and Traps

  1. A static method cannot be abstract in Java. For refer to this discussion on Stackoverflow for rationale behind it. If you need the two together, what you are looking is probably singleton object with inheritance.

  2. Singleton objects are more flexible and useful than static methods in Java. Please refer to Singleton Pattern Versus Static Class for more details.

  3. Override is a concept specific to methods in class in Java. A variable in a class cannot be override in Java. You have to either change the value of the variable to the one want in a subclass (if you don't need a different type) or define a variable with the same name in the subclass to hide the variable in the super class (if you need a different type).

  4. The methods hashCode and equals belong closely together. You should override both or neither. Please refer to Java equals() and hashCode() Contracts and Why do I need to override the equals and hashCode methods in Java? for more discussions. Notice that this principle applies to other OOP programming languages as well, not just Java.

Comments