Java 学习笔记

Class accessibility modifiers:

- final: complete class, can't be subclassed, and methods can't be overloaded;
- abstract: implete class, must be subclassed before instantiation;
- static: can't be instantiated, can only be accessed by class name;
- default: this class can only be visible in this package;
- public: can be accessbile by other classes, and there should be only one in one file;
- private: only visible to this package;

Member accessibility modifiers:

- public: visible to every class;
- protected: visible to subclasses in other packages and every classes in this package;
- default: only visible to classes in the same package;
- private: not visible to any class outside of this class;
- static: define class rather than instance members and methods;
- final: contants;
- abstract: must be implemented in the subclasses;
- synchronized: only one thread can execute the method, mutual exclusion;
- native: used for methods call implemented in other languages;
- transient: used for ever changing members;
- volatile: used to keep consistency when multiple threads are accessing the same object.

Note:

- accessibility of local variables can't be specified;
- local virables can't be declared static;
- method and member reference rule:
* static/class methods can only reference static varibles;
* non-static/object methods can access static varibles;
* static members can be access through a class reference or object reference.
- abstract classes can't have transient member varibles;

if(condition): the condition variable must be a boolean varible;
continue statement must be used in a loop;
break must be used in a block: such as:
aa: {break aa;} while for contiune
aa: {continue aa;} is incorrect;
- labels can be used to handle the excution trace of the program.

Exceptions and Errors

- checked exceptions: the exception must be dealt with explicitly by the method that throw it;
- unchecked exceptions: the method is not obliged to do with the exceptions;
- try-catch-finally construct;
- throw and throws
* throw is used to throw an exception explicitly in the program;
* throws is used to propagate the exception to the caller of the program.
- throw null will cause a NullPointerException;
- assertion is used to handle program errors.

Inheritance in Java

- A class can only extend only one class, this is called linear implementation;
- inherited, extended members;
- Upcasting: subclass reference can be assigned to base class reference;
- method overriding;
- dynamic method allocation, the method called at runtime is determined by the real object type;
- dynamic binding, the mechanism to dynamically determine which function to call at run time;
rather than at compile time;
- this pointer used for the nonstatic classes and methods;
- method overriding and overloading...

this() and super() construct
- this() is used to access the current object;
- super() is used to access the parent class of this class;
if used it must be the first statement in the body of constructors
it can only be used in a constructor declaration;
- Constructor chaining:
using super() call, the top most parent class can be called, this is the upper chain;
using this() call, (overloaded)constructors in the class can be called.
this() and super() calls can not appear in the same constructor;
- if no this() or super() calls are used, the compiler with implicitely insert one super() call,
this is used to call the default constructor of the super class;
- if the current class has no default constructors, a super() call with parameters must be
explicitely stated, if not, a complie time error will follow;

Interface

- The anatomy of an interface:
* constants;
* methods prototypes;
* nested classes;
* nested interfaces;
- interfaces are inheritely public and abstract;
- the methods in the interface must have public accessibility when implemented in classes;
because changing it will voilate the contract of an interface;
- a class can implement some of the methods in the interface, but this class must be abstract;
- the interface methods can't be declared static, because they must be implemented first in
order to use them, (abstract)
- a class can only provides one implementation even though multiple declarations of a method
exist in multiple interfaces.
- one interface can extend several other interfaces, this is different from class inheritence;
one class can only have one direct super class;
one class can implement several other interfaces;
- Constants in interfaces are inheritely with public, static and final modifiers;

Aggregation and Inheritence

- When B is-a A then B can inherite from A, this complies to OOP phylosophy;
- When B has-a A then B can be implemented by aggregation.
- Polymorphism is achieved through inheritence and interface implementation.
Code relying on polymorphic behavior will still work without any change if new subclasses or
new classes implementing the interface are added.
If no obvious is-a relationship is present, then polymorphism is best achieved by using
aggregation with interface implementation.

No comments:

Post a Comment