> For the complete documentation index, see [llms.txt](https://cs61b-2.gitbook.io/cs61b-textbook-spring-2026/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cs61b-2.gitbook.io/cs61b-textbook-spring-2026/9.-inheritance-ii-extends-casting-higher-order-functions/9.4-higher-order-functions-in-java.md).

# 9.4 Summary

### Natural Order (Python vs. Java)

Today we’ve seen **polymorphism** and **function passing**.

For comparing two objects using an intrinsic order (a.k.a. a natural order), Python uses a form of polymorphism called **operator overloading**. Java instead uses subtype polymorphism.

```python
class Dog:
   def __init__(self, name, size):
       self.name = name
       self.size = size
  
   def __gt__(self, other):
       return self.size > other.size
```

```java
public class Dog implements Comparable<Dog> {
   ...
   @Override
   public int compareTo(Dog uddaDog) {
       return size - other.size;
   }
}
```

Note: Python is duck typed: Do not have to specify if > is available or not.

### Other Orders (Python vs. Java)

For comparing two objects using an alternate order, Python uses **function passing**, i.e. you provide a key function. By contrast, Java uses subtype polymorphism, just like for intrinsic orders. We package the comparison function in a `Comparator` object.

```python
class Dog:
   def __init__(self, name, size):
       self.name = name
       self.size = size

def name_len(dog):
   return len(dog.name)

max_dog=max(doglist, key=name_len)
```

```java
public static class NameComparator 
          implements Comparator<Dog> {
   @Override
   public int compare(Dog a, Dog b) {
       return a.name.compareTo(b.name);
   }
}
```

#### Writing Library Functions

If we want to write the code that actually uses a `Comparable` or `Comparator`, then we'll find the method specifications get a little vexing, for example:

```java
public class Maximizer {
   public static <T extends Comparable<? super T>> T max(T[] items) {
		...
   }
}
```

Luckily for you, you won't have to deal with this much, other than at the end of Project 1B.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://cs61b-2.gitbook.io/cs61b-textbook-spring-2026/9.-inheritance-ii-extends-casting-higher-order-functions/9.4-higher-order-functions-in-java.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
