The follows are derived from Java Programming (Poornachandra Sarang) 

 

 

To be memorized: 

Polymorphism 

Q: How does a compeller know which function to use facing over overwriting? 

A: early binding, check the reference to the object that calls the function

 

Term: Binding - connecting object reference to the function body

early binding - binding when compelling

late binding - binding when running

 

Super class and subclass

when call from super class, the implementation in the subclass will not be called; but not vise versa: when call from subclass, the implementation in the super class could be called by "super"; 

 

upcasting is always permitted, but not vise versa

 

Prevent from overwriting: final modifier

 

Constructor for Copy: used for deep copy

 

  1. 1public class TimeOfDay{  
  2.   2.      private int hour, mins;  
  3.   3.      public TimeOfDay (int hour, int mins){  
  4.   4.           this.hours = hour;  
  5.   5.           this.mins = mins;  
  6.   6.      }  
  7.   7.      public TimeOfDay (TimeOfDay other){  
  8.   8.           this(other.hour, other.mins);  
  9.   9.      }  
  10.   10. }  

 Final

 

String s1 = "abc";

 

String s2 = "abc";

s1 and s2 point to the same memory address in that the String is a final class and  cannot be modified.

 

the private and static methods cannot be overwrite int the subclass, so they are intrinsic final methods.

 

Three advantages of final methods:

 

1. prevent from overwriting

 

2. tell the compeller the calling to final methods does not require dynamic binding

 

3. transfer ordinary calling from inline calling.

 

note: 1. Java forbids marking methods as inline calling.

 

           2. Naming tradition for final variables: eg. final String A_FINAL_STRING = "";

 

           3. a final variable can only be assigned once: when stated or initialized in the constructor

 

           4. a null final variable must be initialized in Constructor 

 

           5. if final variables save references to the object, then the reference must stay the same, while the object variables can be modified.

 

static constructor

 

static {

  radius = 5;

}

 

static constructor can be used for Database Connector etc.

 

The static constructor cannot be placed with too much codes, for its size is limited to 64KB

 

interface

 

contains constant value and abstract methods (not marked patently)

 

cannot contains implementations

 

Interfaces contains no methods are called marker interface. eg. Serializable

 

If a class does not implement all the methods in an interface, the class becomes a abstract class.

 

an interface can be public or default

 

an interface can extends another (or others)

 

Nested Class

 

An inner class can not only be stated in another class, but in a method or a parameter.

 

Three advantage for nested class:

 

1. logistic classified classed 

 

2. better encapsulation

 

3. easy to understand and maintain

 

Nested Class Category:

 

1. static class

 

2. non-static class

     2.1 local class

 

     2.2 anonymous class

 

1. OuterClassName.StaticNestedClassName ClassObjectName = new OuterClassName.StaticNestedClassName();

Similar to ordinary classes

 

2.1 a local class cannot contain static methods and variables.

 

 can be used as iterator class, and can be initialized by this.new

 

In most times, inner class will not be stated as public class.

 

 Outer outer = new Outer();

 Outer.Inner inner = new outer.Inner();

Note: the inner class and outer class cannot share the same name

 

the compeller will generate a .class file for each inner class. eg. Outer$Inner.class

inner class could use variables and other classes in the outer class

an inner class could be a interface, which will be implemented by another inner class.

inner class could be abstract

a static inner class is a toplevel class in fact.

"static" modifier could be used to inner class, but not outer class

Anonymous Class: 

 

  1. 1. button.addActionListener(new ActionListener() {  
  2. 2.     public void actionPerformed(ActionEvent e){  
  3. 3.         System.out.println(" ");  
  4. 4.     }  
  5. 5. }  

 or new Runnable(){public void run(){}}

 

Another application scenario: Java Collections Framework

 

  1. 1. Vector friends = new Vector(4) {  
  2. 2.     add("S");  
  3. 3.     add("N");  
  4. 4.     add("D");  
  5. 5.     add("B");  
  6. 6.     add("C");  
  7. 7. }  

the anonymous class will be named as EnclosingClassName$1.class

Local class

e.g. 

  1. 1public class Outer{  
  2. 2public Outer(){  
  3. 3class Local(){  
  4. 4//some implementation  
  5. 5. }  
  6. 6new Local();  
  7. 7. }  
  8. 8. }  

Note: new and super are not permitted in the local class.

static is not allowed

public, protected, private and static are not allowed.

a inner class in a method have access to parameters with final modifier only.