Java Key Features
General Info
Return to top
static
- static variable/method or class variable/method: shared among all instances of a class.
- can be called directly with class name, like Myclass.number if a number is a static variable.
- static initializer: a static block, not inside a method, called once and first when the class is loaded.
- A static class can be declared inside an interface.
Return to top
final
- final class cannot be subclassed.
- final method cannot be overridden.
- A final variable is a constant.
Return to top
abstract
- If a class contains one or more method signatures without implementation, that class is called abstract class
- A class can be an abstract by using abstract modifier.
- A method can be an abstract by using abstract modifier.
- An abstract class serves as a super class.
- An abstract class can have member variables, concrete methods and main() method.
- An abstract class can be executed if it contains a main() method.
- You cannot instantiate an abstract class.
Return to top
interface
- Like an abstract class, but no concrete methods and member variables are constant by default.
- An interface can extend another interface.
- An interface can have a static class declared inside.
- A class can implement multiple interfaces, separated by comma.
- An interface is used to declare methods that one or more classes are expected to implement.
- An interface can work as a guideline for developers.
- An interface can be designed to adapt different classes to work together.
Return to top
Access control modifiers
- public
- protected
- default(no modifier)
- private
- From more generous to less generous: public--> protected--> default--> private.
Return to top
"==" vs. equals
- Both for determining if reference values refer to the same object.
- equals() tests contents of two separate objects.
- "==" tests memory address.
- equals() is designed in the Object class and may be overridden.
- equals() may be overridden with hashcode() method.
Return to top
Wrapper classes
- 8 primitive types have corresponding wrapper classes.
- Many methods designed to transfer primitive to wrapper or wrapper to primitive.
- New features in jdk 1.5 may allow primitive to wrapper to assign each other automatically
Return to top