Skip to content

CS61B 2018 Lecture 9 Extends, Casting, Higher Order Functions #89

@poanc

Description

@poanc

Summary

  • More Inheritance
    • Extends
    • super
    • Constructors Are Not Inherited
    • The Object Class
    • Encapsulation
    • How Inheritance Breaks Encapsulation
    • Type Checking and Casting
      • Expressions
      • Casting
    • Dynamic Method Selection and Casting Puzzle
    • Higher Order Functions
  • Conclusion: Inheritance Cheatsheet
  • Important notes:
    1. The extends keyword defines "is-a", or hypernymic relationships. A common mistake is to instead use it for "has-a", or meronymic relationships.
    2. Casting cause no change to the type of variable, it can only cheat the compiler while the compiler-time.
  • Summary notes about Dynamic Method Selection
    1. Compiler allows method calls based on compile-time type of variable.
    2. If overriden, decide which method to call based on run-time type of variable.
    3. If there is no overriding, no dynamic method selection occurs.

Extends

In the previous, we've discussed the concept of implement and inheritance. In this lecture, the more inheritance will be explored.
2019-02-18 10 18 43
Use extends, we can use the methods that come from the super class, but also can implement some own methods.
2019-02-18 10 18 58
By using the extends, subclasses inherit all members of the parent class. "Members" includes:

  • All instance and static variables
  • All methods
  • All nested classes

Note that constructors are not inherited, and private members cannot be directly accessed by subclasses.

public class RotatingSLList<Item> extends SLList<Item>{
    public void rotateRight() {
        Item x = removeLast();
        addFirst(x);
    }
}

super()

image
We would like to use the removeLast method from superclass but add some features. We can just add the code from super class, but there are private properties and private methods we cannot access.
Thus, the super keyword can be used to get the method from the superclass.

public class VengefulSLList<Item> extends SLList<Item> {
    SLList<Item> deletedItems;

    public VengefulSLList() {
        deletedItems = new SLList<Item>();
    }

    @Override
    public Item removeLast() {
        Item x = super.removeLast();
        deletedItems.addLast(x);
        return x;
    }

    /** Prints deleted items. */
    public void printLostItems() {
        deletedItems.print();
    }
}

Constructors Are Not Inherited

Once we want to create a subclass object, the compiler will automatically build a superclass for us whatever the super() keyword is in the subclass' constructor.
2019-02-18 10 57 08

However, if we would like to build a subclass object with parameters, the super() keyword with the parameter must be included in subclass constructor.
2019-02-18 10 57 55

The Object Class

Every class is Object, even we do not extends Object explicitly.
image

Important Note: The extends keyword defines "is-a", or hypernymic relationships. A common mistake is to instead use it for "has-a", or meronymic relationships.

image

Encapsulation

To be a great programmer, someone has to learn handling complexity propriately. Some tools for managing complexity below:
image

Module: A set of methods that work together as a whole to perform some task or set of related tasks

image

Java is has a nice encapsulation feature because it has private properties that do not allow outsider to look inside.
image

How Inheritance Breaks Encapsulation

Suppose we had the following two methods in a Dog class. We could have implemented bark and barkMany like so:

public void bark() {
    System.out.println("bark");
}

public void barkMany(int N) {
    for (int i = 0; i < N; i += 1) {
        bark();
    }
}

Or, alternatively, we could have implemented it like so:

public void bark() {
    barkMany(1);
}

public void barkMany(int N) {
    for (int i = 0; i < N; i += 1) {
        System.out.println("bark");
    }
}

From a user's perspective, the functionality of either of these implementations is exactly the same.
However, here is the questions we had to think about:
image
We had to turn to dynamic method selection.

The answer is A : As a dog, I say: bark bark bark

And here is another question:
image
image

Type Checking and Casting

Recall that dynamic method lookup is the process of determining the method that is executed at runtime based on the dynamic type of the object. Specifically, if a method in SLList is overridden by the VengefulSLList class, then the method that is called at runtime is determined by the run-time type, or dynamic type, of that variable.

image
2019-02-20 9 25 42

image

Expressions

2019-02-20 9 31 03

2019-02-20 9 31 17

Casting

image
image

Dynamic Method Selection and Casting Puzzle

Some practice about method overriding and overloading
image
2019-02-20 9 53 17
2019-02-20 9 53 25

IMPORTANT:Casting cause no change to the type of variable, it can only cheat the compiler while the compiler-time.

Higher Order Functions

Higher Order Function: A function that treats another function as data. e.g. takes a function as input.
example:

def tenX(x):
    return 10*x

def do_twice(f, x):
    return f(f(x))

print(do_twice(tenX, 2)

In old school Java (Java 7 and earlier), memory boxes (variables) could not contain pointers to functions. What that means is that we could not write a function that has a "Function" type, as there was simply no type for functions.

image
image
image

Inheritance Cheatsheet

image

Dynamic Selection Method Selection Puzzle

截圖 2020-05-28 上午9 00 27

截圖 2020-05-28 上午9 00 38

截圖 2020-05-28 上午9 00 50

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions