Contents

61B-8: Inheritance, Implements

Contents

/61b-8/image.png but hard to maintain!

Hypernyms, Hyponyms, and Interface Inheritance

/61b-8/image-1.png

interface

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public interface List61B<Item> {
   public void addFirst(Item x);
   public void addLast(Item y);
   public Item getFirst();
   public Item getLast();
   public Item removeLast();
   public Item get(int i);
   public void insert(Item x, int position);
   public int  size();
}

Overriding vs. Overloading

/61b-8/image-2.png override注意加上@Override!!!

Interface Inheritance

基类存放指针问题 Answer: If X is a superclass of Y, then memory boxes for X may contain Y.

  • An AList is-a List.
  • Therefore List variables can hold ALList addresses.

Implementation Inheritance: Default Methods

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public interface List61B<Item> {
   public void addFirst(Item x);
   public void addLast(Item y);
   public Item getFirst();
   public Item getLast();
   public Item removeLast();
   public Item get(int i);
   public void insert(Item x, int position);
   public int size();  
   default public void print() {
      for (int i = 0; i < size(); i += 1) {
         System.out.print(get(i) + " ");
      }
      System.out.println();
   }
}

Static and Dynamic Type, Dynamic Method Selection ⚠️ ⚠️ ⚠️

/61b-8/image-3.png /61b-8/image-4.png

More Dynamic Method Selection, Overloading vs. Overriding ⚠️ ⚠️ ⚠️

紧紧盯住细节! /61b-8/image-5.png

Interface vs. Implementation Inheritance

/61b-8/image-6.png /61b-8/image-7.png