Wednesday, November 28, 2012

11/28/2012 - present

1. Why prefer composition to inheritance?


2. Anonymous Inner Class has the same name, say ClassA, with a class - ClassA means this anonymous class is the subclass of ClassA .


3. synchronized keyword can only be applied to a method or a block.
not even a class or a data field!!!!!


4. every subclass must call either super() or super(someparameters) explicitly,
otherwise super(); will be added to the first line of subclass constructor by compiler.

EX:

package b;
import a.A;

public class B extends A{
B(int i)
{
super(i); //call super(i) explicitly, otherwise, super() would be called.
System.out.println("B");
}
public static void main(String[] args){
new B(1);
}
}
--------------------------------------------------------

package a;
public class A{
A( ){System.out.println("A");}
// (default) only accessible within the same package. So, if B didn't call super(i) explicitly, super(); would be called and then compile error would occur.
protected A(int a)
{
System.out.println("aA");
}
public void  print()
{
System.out.println("A");
}
}

Result:
aA
B

5. The return type should be same for overridden and overriding method.
Having the same signature is not enough for overriding. Return type must be the same.

Overriding:  same-return-type same-method-Name (same-parameters) {}

Overloading:  same-method-name (){}

public void  print()
{
System.out.println("A");
}
int print(int i)
{
return i;
}

Inheritance
1. Remember : variables are SHADOWED and methods are OVERRIDDEN.
Which variable will be used depends on the class that the variable is declared of.
Which method will be used depends on the actual class of the object that is referenced by the variable.  


2. instanceof
if(a instanceof B)
to use instanceof a,B must be compatible, which means that the actual type of a, say A, either extends B or implements B or is B, otherwise, compile error will occur.
c instanceof Interf 在c不为final的情况下可以是compatible的, 因为c的subclass可能会implements Interf. 但是不可能extends B了 因为只能extends一个.


3.  Fields in an interface are implicitly public, static and final. Although you can put these words in the interface definition but it is not a good practice to do so.  

4.  Having ambiguous fields or methods does not cause any problems by itself but referring to such fields/methods in an ambiguous way will cause a compile time error
        What, if anything, is wrong with the following code?
// Filename: TestClass.java
class TestClass implements T1, T2{
   public void m1(){}
}
interface T1{
   int VALUE = 1;
   void m1();
}
interface T2{
   int VALUE = 2;
   void m1();
}
//it can be compiled
//TestClass tc = new TestClass();
//System.out.println(( ( T1) tc).VALUE); 
// tc.m1() is also fine because even though m1() is declared in both the interfaces, the definition to both resolves unambiguously to only one m1(), which is defined in TestClass.  

5. There is no construct like super.super. So, there is no way you can access m1() of A from C.   //even you cast c to A, you still call the method1 in C. 

class A{
   public void m1() {   }
}
class B extends A{
   public void m1() {   }
}
class C extends B{
   public void m1(){
   }
    public static void main(String...args)
 {
  C c =new C();
  System.out.print(c.method1());
  System.out.print(((A)c).method1());//even you cast c to A, you still call the method1 in C.
 }
}

6. A class cannot override the super class's constructor.
Because constructors are not inherited.

7. Interface cannot "implement" anything. It can extend multiple interfaces. The following is a valid declaration : 
interface I1 extends I2, I3, I4 { }

8. Keep in mind that static methods are not overridden, they are shadowed.
In computer programmingvariable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope. This outer variable is said to be shadowed.

9. Order of initiation of an instance
1. Static blocks of the base class (only once, in the order they appear in the class).

2. Static blocks of the class.

3. Non-static blocks of the base class.

4. Constructor of the base class.

5. Non-static blocks of the class.

6. Constructor of the class.

7. Derived class's static or non-static blocks are not executed if that class is not being used.




No comments:

Post a Comment