JAVA OOD 小結(jié)

Primitive data type, and object data type

Student s = new Student():
compiler will create a student object in heap, and then pass a reference of the heap space to variable s (it's the address).

Person p = new Student(); -> works
Student s = new Person(); -> not work,
Person p; p = s; -> works, 
Student s; s = p; -> not work
等號左邊要包括右邊

Protected: same packeage, subclass, same class
Package: same package, same class
These two are not recommended, as other class in same package can access.

Everything extend from Java Object class

Object creation: from subclass follow constructor all the way to object, and then initialize one by one.
From object, to subclass息尺。

Compiler rules:

  1. if no super class, extends object
  2. If no constructor, compiler gives one for you
  3. First line in constructor, must be either this()->same class, another constructor, or super(). Otherwise compiler will do super()

One of the confusing thing is:

public class Person {
    private String name;
    public Person(String s){
        this.setName(s);
    }
    public Person(){}
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

public class Student extends Person{
    
        private String name;
        public Student(){
        super("Lily");
        this.name = "Amy";
    }
    public static void main(String[] args) {
        Student s = new Student();
        System.out.println(s.getName());
        System.out.println(s.name);
    }

}

這段代碼的輸出是先Lily烦租,再Amy畜挨。子類class繼承了父類的所有方法與變量,但如果不加聲明 (不在子類中定義一個string name)噩斟,子類用的是父類的name變量。而s.getName()是子類調(diào)用父類的方法孤个,表示取得了父類的name剃允。在子類調(diào)用Super時,子類把父類實例化齐鲤,同時賦值了父類的name

Overloading: Same class has same method, with different parameters. Return type can be different, as long as the parameters are different. Java compiler only identifies parameter list.

Overriding: Sub-class has same signature (name, parameters list) as super class

Polymorphism: what it gives us, is the ability to keep alll of our objects in one big collection, and then call methods on every single obect, and appropriate method will be make sure to get executed.

Polymorphism:

Compile time rules:
  1. compiler only knows the reference type:
Person p = new Student() -> compile only knows p is a reference to Person object type
  1. Compiler can only look at reference type class, for method, and Compiler outputs a method signature
Run time Rules
  1. Follow exact Runtime type of object,
  2. Find the method that matches Compiler signature, on Runtime object.
Person p = new Student() -> Run time now follows the Student object, 
Abstract Class vs Interface

Both two can achieve the following:

  1. Force subclasses to have/override this method,
  2. Stop having actual instantiated objects but keep having references.

Interface only provides "Final Static" Variables and Abstract Methods, it doesn't provide common codes. Interface is implicitly abstract, you don't need to declare it. Interface doesn't contains constructor.
Interface is a good way, to force Sub-classes to implement the functions.

However Abstract Class can contain Abstract Methods, and it can also contains common codes (common methods). Common methods are the methods that Sub-classes can just extend, and don't need to implement themselves.
If one method in class in Abstract, class must be Abstract. Abstract class cannot be instantiated. To use Abstract class, you have to extend it, and must override the Abstract method.

Class Relationship

Association: Student select a class
Aggregation: has a relationship, one to many mapping
Combination: has a relationship, one to one mapping,
Dependence: supplier and customer,
Inheritance: Is a relationship

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末斥废,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子给郊,更是在濱河造成了極大的恐慌营袜,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件丑罪,死亡現(xiàn)場離奇詭異荚板,居然都是意外死亡,警方通過查閱死者的電腦和手機吩屹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門跪另,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人煤搜,你說我怎么就攤上這事免绿。” “怎么了擦盾?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵嘲驾,是天一觀的道長。 經(jīng)常有香客問我迹卢,道長辽故,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任腐碱,我火速辦了婚禮誊垢,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己喂走,他們只是感情好殃饿,可當我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著芋肠,像睡著了一般乎芳。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上帖池,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天奈惑,我揣著相機與錄音,去河邊找鬼碘裕。 笑死携取,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的帮孔。 我是一名探鬼主播雷滋,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼文兢!你這毒婦竟也來了晤斩?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤姆坚,失蹤者是張志新(化名)和其女友劉穎澳泵,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體兼呵,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡兔辅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了击喂。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片维苔。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖懂昂,靈堂內(nèi)的尸體忽然破棺而出介时,到底是詐尸還是另有隱情,我是刑警寧澤凌彬,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布沸柔,位于F島的核電站,受9級特大地震影響铲敛,放射性物質(zhì)發(fā)生泄漏褐澎。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一原探、第九天 我趴在偏房一處隱蔽的房頂上張望乱凿。 院中可真熱鬧顽素,春花似錦咽弦、人聲如沸徒蟆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽段审。三九已至,卻和暖如春闹蒜,著一層夾襖步出監(jiān)牢的瞬間寺枉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工绷落, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留姥闪,地道東北人。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓砌烁,卻偏偏與公主長得像筐喳,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子函喉,可洞房花燭夜當晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容