> For the complete documentation index, see [llms.txt](https://cs61b-2.gitbook.io/cs61b-textbook-fall-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-fall-2026/10.-inheritance-ii-extends-casting-higher-order-functions/10.4-higher-order-functions-in-java.md).

# 10.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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cs61b-2.gitbook.io/cs61b-textbook-fall-2026/10.-inheritance-ii-extends-casting-higher-order-functions/10.4-higher-order-functions-in-java.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
