Sunday, January 31, 2016

[Java] Leaning notes : Encpsulation, inhrit and polimophic




 From today, I started to collect my learning notes about Java.
Encapsulation, inherit and polymorphic

the three most important characteristics in Java

  Encapsulation

for : 
  1. dis-couple : reduce the dependency between the classes   
  2. easy to change the intern structure but not block the user of the class
  3. control of the access of the members  
  4. hind some informations

the principle usage of encapsulation is  the getter and setter   

 Inherit

 WHY : reuse the code! Inherit defines the relation (couple) between the classes. 
HOW :  
  1. the sub class has the parent's non - private members and methods 
  2. the sub class extends its own members and methods
  3. the sub class can override parent's methods
 Private :  just the class itself
 Protected : can be accessible by the sub classes and by the class in the same package
 A<--B : B is A (dog is animal)
When the parent changes, the sub classes need change too. So this damages the encapsulation.
WHEN : if we need transform type from sub class to parent, we need to use Inherit.  

 Polymorphic

 WHAT : we have a reference of an object, and this reference can refer to A or B. So the method called when we do Ref.method is not fixed in compilation, it's decided in runtime. 

 HOW : 
1. Inherit + transform to supper class + override method
              A
             /  \
           B    C
A a = new A();
A b = new B();
B b1 = new B();
A c = new C();

public class A{
    public f1(){...}//1
    public f2(){...}//2


public class B{
    public f1(String s){.....}//3
    public f2(){.......} //4
}

public class Test{
    ......
  A b = new B();
  b.f1(); -------> A.f1() : f1() is different from f1(String) -----> this is overlaod
  b.f2(); -------> B.f2() : f2() is override by B -----> this is override
    ......
}

Reference is parent type but the object is sub class type (A b = new B()): 
1. b ca only access A 's methods.
2. if the methods are override by B, the sub classes' methods will be called.

Advantages : the same operation apply on different classes will have different results, so we can use the same logic to trait different classes, (animal 's sub classes bird and dog can have different behaviors for the same method "eat()")



2.Interface (Multi-inherit)

 Class A | + void show(D obj){"A - D"}
                | + void show(A obj){"A - A"}
|      
|        
|           
class B | + void show(B obj){"B - B"}
               | + void show(A obj){"B - A"}
               |  + void show(D obj){"A - D"}//this tow methods can be accessed by B!!
                | + void show(A obj){"A - A"}//


|       \
|          \
|             \ 
|               \
classC     classD 

test: 
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();


a1.show(b); A A
a1.show(c);A A
a1.show(d); A D

a2   ..... b B A // B 's instance but reference type A, so it can just call A' s methods, because input b is type A, so it will call the A.show(A), but we found this method is override in B, so we call B.show(A)
a2   ..... c B A// same
a2   ..... d A D

b   ..... b B B
b   ..... c B B// B's object and B's reference, so it can call all method in B and in A. So we try :  B.show(C), there isn't this method, so we transport C to his father B, and we found B has the method B.show(B)
b   ..... d   A D// 




No comments:

Post a Comment