- 使用 Annotation "@Override" 標示是個 overriding method,
若 superclass 不包含同 signature 的 method, 會出現compiler error.
- Defining a Method with the Same Signature as a Superclass's Method
Superclass Instance Method Superclass Static Method Subclass Instance Method Overrides Generates a compile-time error Subclass Static Method Generates a compile-time error Hides
- Overriding:
不允許 throw new checked exception,
但 unchecked exception 可以 (e.g. RuntimeException). - Covariant return type:
the return type of the overriding method is
a subtype of the return type of the overridden method. - e.g.
class AA
class A extends AA
class B extends A
class C extends B
class superTest{
public A method1(){
return new A();
}
}
class subTest extends superTest{
public B method1(){ // or public C method1()
return new C();
}
}
- Method inner class
- e.g.
void method1(){
class A {
// some implement.
}
} - Singleton Pattern
- Constructor 設為 private
- Method getInstance() 設為 static 及 synchornized
- Override method clone()
- Cohesion: the whole of a class sticks together.
Coupling: the inter dependency between 2 or more classes. - Ideally design: high cohesion and loose coupling.
---
沒有留言:
張貼留言