Inheritance Note

Hypernym and Hyponym

We use the word hyponym for the opposite type of relationship.

  • “dog”: Hypernym of “poodle”, “malamute”, “dachshund”, etc.
  • “poodle”: Hyponym of “dog”

Hypernyms and hyponyms comprise a hierarchy.

  • A dog “is-a” canine.
  • A canine “is-a” carnivore.
  • A carnivore “is-an” animal.

Interface

  • Interface is a specification of what a class is able to do, not how to do it.

Overriding vs. Overloading

  • If a “subclass” has a method with the exact same signature as in the “superclass”, we say the subclass overrides the method.
  • Methods with the same name but different signatures are overloaded.
  • the @Override annotation: the only effect of this tag is that the code won’t compile if it is not actually an overriding method.

Interface Inheritance

Specifying the capabilities of a subclass using the implements keyword is known as interface inheritance.

  • Interface: The list of all method signatures.
  • Inheritance: The subclass “inherits” the interface from a superclass.
  • Specifies what the subclass can do, but not how.
  • Subclasses must override all of these methods!

Implementation Inheritance: Default Methods

Use the default keyword to specify a method that subclasses should inherit from an interface.

  • If you don’t like a default method, you can override it.

Static and Dynamic Type, Dynamic Method Selection

Every variable in Java has a “compile-time type”, a.k.a. “static type”.

  • This is the type specified at declaration.

Variables also have a “run-time type”, a.k.a. “dynamic type”.

  • This is the type specified at instantiation (e.g. when using new).
  • Equal to the type of the object being pointed at.

Suppose we call a method of an object using a variable with:

  • compile-time type X
  • run-time type Y

Then if Y overrides the method, Y’s method is used instead.

  • If we have a variable has static type X and dynamic type Y, then if Y overrides the method, Y's method is used instead.
  • At compile time, the compiler will verify X has a method that can handle the given parameter. If multiple methods can handle, it records the most specific one.
  • At runtime, if Y overrides the recorded signature, use the overridden method.

Implementation Inheritance: Extends

When a class is a hyponym of an interface, we used implements. If you want one class to be a hyponym of another class, you use extends.

  • All instance and static variables.
  • All methods.
  • All nested classes.
    Constructor are not inherited.

Type Checking and Casting

  • Compiler allows method calls based on compile-time type of variable.
  • Compiler also allows assignments based on compile-time types.

Expressions have compile-time types:

  • An expression using the new keyword has the specified compile-time type.
  • Method calls have compile-time type equal to their declared type.

Java has a special syntax for specifying the compile-time type of any expression.

  • Put desired type in parenthesis before the expression.
    • Compile-time type Dog: maxDog(frank, frankJr);
    • Compile-time type Poodle: (Poodle) maxDog(frank, frankJr);

Subtype Polymorphism

Polymorphism: “providing a single interface to entities of different types”

Interfaces and Abstract Classes

implements and extends can be used to enable interface inheritance and implementation inheritance.

  • Interface inheritance: What (the class can do).
  • Implementation inheritance: How (the class does it).

Interfaces may combine a mix of abstract and default methods.

  • Abstract methods are what. And must be overridden by subclass.
  • Default methods are how.
  • Unless you use the keyword default, a method will be abstract.
  • Unless you specify an access modifier, a method will be public.
  • Can provide variables, but they are public static final.
  • A class can implement multiple interfaces.
  • Cannot be instantiated.
  • Java 9 added private methods to interfaces.

Abstract classes are an intermediate level between interfaces and classes.

  • Cannot be instantiated.
  • Can provide either abstract or concrete methods.
  • Use abstract keyword for abstract methods.
  • Use no keyword for concrete methods.
  • Can provide variables (any kind).
  • Can provide protected and package private methods.

Abstract classes are often used as partial implementations as interfaces.

Interfaces:

  • Primarily for interface inheritance. Limited implementation inheritance.
  • Classes can implement multiple interfaces.

Abstract classes:

  • Can do anything an interface can do, and more.
  • Subclasses only extend one abstract class.

Generalized Comparison

Comparables

The industrial strength approach: Use the built-in Comparable interface.

Already defined and used by tons of libraries. Uses generics.

public interface Comparable<T> {
     public int compareTo(T obj);
} 
  • Lots of built in classes implement Comparable (e.g. String).
  • Lots of libraries use the Comparable interface (e.g. Arrays.sort)
  • Avoids need for casts.
public class Dog implements Comparable<Dog> {
    public int compareTo(Dog uddaDog) {
        return this.size - uddaDog.size;
    }

Comparators

In some languages, we’d write two comparison functions and simply pass the one we want :

  • sizeCompare()
  • nameCompare()

The standard Java approach: Create sizeComparator and nameComparator classes that implement the Comparator interface.

  • Requires methods that also take Comparator arguments
public interface Comparator<T> {
    int compare(T o1, T o2);
}

public class Dog implements Comparable<Dog> {
  private String name;
  private int size;
 
  public static class NameComparator implements Comparator<Dog> {
    public int compare(Dog d1, Dog d2) {
        return d1.name.compareTo(d2.name);
    }
  }
  ...
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末押搪,一起剝皮案震驚了整個濱河市终蒂,隨后出現(xiàn)的幾起案子肤视,更是在濱河造成了極大的恐慌,老刑警劉巖因苹,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件弊琴,死亡現(xiàn)場離奇詭異,居然都是意外死亡祈秕,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進店門雏胃,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人志鞍,你說我怎么就攤上這事瞭亮。” “怎么了固棚?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵统翩,是天一觀的道長。 經常有香客問我此洲,道長厂汗,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任呜师,我火速辦了婚禮娶桦,結果婚禮上,老公的妹妹穿的比我還像新娘汁汗。我一直安慰自己衷畦,他們只是感情好,可當我...
    茶點故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布知牌。 她就那樣靜靜地躺著祈争,像睡著了一般。 火紅的嫁衣襯著肌膚如雪角寸。 梳的紋絲不亂的頭發(fā)上菩混,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天忿墅,我揣著相機與錄音,去河邊找鬼沮峡。 笑死疚脐,一個胖子當著我的面吹牛,可吹牛的內容都是我干的帖烘。 我是一名探鬼主播亮曹,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼秘症!你這毒婦竟也來了照卦?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤乡摹,失蹤者是張志新(化名)和其女友劉穎役耕,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體聪廉,經...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡瞬痘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了板熊。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片框全。...
    茶點故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖干签,靈堂內的尸體忽然破棺而出津辩,到底是詐尸還是另有隱情,我是刑警寧澤容劳,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布喘沿,位于F島的核電站,受9級特大地震影響竭贩,放射性物質發(fā)生泄漏蚜印。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一留量、第九天 我趴在偏房一處隱蔽的房頂上張望窄赋。 院中可真熱鬧,春花似錦肪获、人聲如沸寝凌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽较木。三九已至,卻和暖如春青柄,著一層夾襖步出監(jiān)牢的瞬間伐债,已是汗流浹背预侯。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留峰锁,地道東北人萎馅。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓,卻偏偏與公主長得像虹蒋,于是被迫代替她去往敵國和親糜芳。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,292評論 0 10
  • 引用 @Rio 的看法:我覺得 Python 不會像 PHP 那樣流行,根本原因在于部署的難易程度晃虫。 PHP 從語...
    一只好奇的茂閱讀 1,299評論 3 19
  • 感恩好友分享的神奇單方皆撩,治愈久咳不止。 感謝讓這個單方傳播給好友的所有親們哲银。 謝謝婆婆家的土雞蛋和親戚家的蜂蜜扛吞,能...
    若水柳柳柳閱讀 126評論 0 0
  • 前 這是第四次來這個房間,從第一次來到現(xiàn)在屋子里始終散發(fā)著一股特殊的氣味荆责。 我從未懷疑過他的身份滥比,他給我心靈巨大的...
    林嘉瑞閱讀 485評論 0 4
  • 在做一件事情的時候守呜,在沒有做到最好之前,再苦再累山憨,也別說我已經很努力了!因為這樣說弥喉,其實是在告訴別人:我很無能郁竟! ...
    小一哥閱讀 204評論 0 0