-
Notifications
You must be signed in to change notification settings - Fork 4
Description
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:
- The extends keyword defines "is-a", or hypernymic relationships. A common mistake is to instead use it for "has-a", or meronymic relationships.
- 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
- Compiler allows method calls based on compile-time type of variable.
- If overriden, decide which method to call based on run-time type of variable.
- 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.

Use extends, we can use the methods that come from the super class, but also can implement some own methods.

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()

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.

However, if we would like to build a subclass object with parameters, the super() keyword with the parameter must be included in subclass constructor.

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

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.
Encapsulation
To be a great programmer, someone has to learn handling complexity propriately. Some tools for managing complexity below:

Module: A set of methods that work together as a whole to perform some task or set of related tasks
Java is has a nice encapsulation feature because it has private properties that do not allow outsider to look inside.

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:

We had to turn to dynamic method selection.
The answer is A : As a dog, I say: bark bark bark
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.
Expressions
Casting
Dynamic Method Selection and Casting Puzzle
Some practice about method overriding and overloading



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.
















