Wednesday, January 21, 2015

[Java]Reusing Classes


The article is mainly the summary of book "Thinking in Java" from Page 237 to Page 275.

Composition, Inheritance, Delegation:

  1. When the relation between two classes is "has a", you can use composition, namely contains a instance of other class in other class. 
  2. If the relationship is "is a", you should use inheritance, derive a class form the base class.
  3. The third relationship, which is not supported by java directly, is delegation. It is something like a layer relationship. Place an object in the class you are building, but at the same time you expose all the method from the member object to your new class. You can use the composition style, and create the method with the same name, arguments and return value to wrap the method in that object. The methods can be forward to the underlying objects. (Consider the database design, it is a layered structure)
  4. When create an object of derived class, it contains a sub object of the base class, the base class is wrapped within the derived class, but the interface is exposed to the derived, the derived class does not need to use instance to access those interface. 
  5. Java automatically insert calls to the base-class constructor in the derived class constructor. But when there is not default base-class constructor, or if you want to call base-class constructor that has argument, you must explicitly write the the call of base-class constructor with super keyword.
  6. Overloading methods in derived class will not hide the base-class versions. You can override the method in base-class.
  7. Derived class has the access to the protected member in base-class.
  8. Upcasting, convert a reference of derived class to base class is always safe since we cast from a superset, 
Final:
  1. When final is used with a primitive type, it means the value of that variable is constant and cannot be changed during the running of the programming.
  2. When a reference is final, it means that we can not change it to point to another object, but we can modify the object itself. Java does not provide a way to make arbitrary object a constant.
  3. Notice that final does not mean that we know the value of that variable, for example, we can initialise it with a random number.
  4. Java allows blank finals, which are fields that are declared as final but are not given a initialise value. In all cases, the blank final must be initialise before it is used, and the compiler ensure this. Notice that if variable is declared as static final, it must be initialise when declare.
  5. If argument is declared as final, then in that function, if it is reference, we cannot change it to point to another object, if it is primitive type, we cannot change the value of it.
  6. The reasons for final methods, first it cannot be overridden in all derived class; second, in past, in can improve efficiency since it can turn calls into inline calls, but now, it is no longer necessary. Notice, all private functions are implicitly final since no one can override it. If we define a same function in derived class, they are different functions, and have no relationship, since derived class have no access to private methods in base-class.
  7. If a class is final, then it cannot be inherited, all fields can be final or not final, but all methods are implicitly final since no one can override it.
Initialisation and class loading:
  1. The class is loaded when first it is used, the first use is also where the static initialisation takes place or object is constructed.
  2. First, if it has base class we will go to the base class, until we there is not base-class, then we initialise the static field in the textual order and go from inner to outside.
  3. Second, when the constructor is called, we will first initialise the fields with the default value go to the base class and call the constructor of base class and do the same things until there is no base class.
  4. Last, we assign the each variable its value in textual order and execute the constructor and go down to the derive class, until we finish.

No comments:

Post a Comment