KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • Streams : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Static : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Type Inference : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

References and Polymorphism

public class Person {
void speak() { }
}
public class Student extends Person {
void learn() { }
}
Student student = new Student();
Person person = student;
person.speak();
person.learn();
student.speak();
student.learn();

This lesson combines what we’ve learned previously about polymorphism with what we now know about references. We’ll examine how the type of a reference variable—not the type of what it refers to—determines what methods and field we can access using dot notation. We’ll also use this opportunity to reinforce our developing view of references.

Reference v. Instance Type
Reference v. Instance Type

When we introduced polymorphism and “is a” relationships, we introduced how the type of a variable and the type of the object stored in it could differ:

Object o = new String("test");

Now, using our more precise terminology, we can be more clear about exactly what is going on here:

public class Person { }

Dot Notation
Dot Notation

One additional piece of Java syntax that should now become more clear is dot notation. We’ve been using it to access object fields and methods:

String s = new String("88");
System.out.println(s.length());

Now we know what is happening! The . causes Java to follow the reference variable to the object it refers to: in this case it follows s to the String we created on the line above. If we create two references that lead to the same place, it will follow either:

String s = new String("88");
String t = s;
System.out.println(s.length());
System.out.println(t.length());

More Reference Examples
More Reference Examples

With a bit more information under our belt, let’s go over a few more important examples of places where references are used.

Pass by Reference
Pass by Reference

When a method is called in Java, what is passed to the method is a reference, not a copy of the object. This allows the method to modify the object. Let’s look at how this works!

public class Example {
public int value = 0;
}
void increment(Example example) {
example.value++;
}
Example e = new Example();
System.out.println(e.value);
increment(e);
System.out.println(e.value);

And now, using a diagram to make the relationships clear visually:

Arrays Store References
Arrays Store References

Java arrays that store objects actually store object references. This has some important implications. Let’s look at an example:

public class Person {
public int age = 0;
Person(int setAge) {
age = setAge;
}
}

Practice: Object Array Shallow Copy

Created By: Geoffrey Challen
/ Version: 2020.10.0

Create a public class named Copier. Copier should provide a single static method named copy that accepts an array of Objects. It should return a shallow copy of the passed array: meaning that the returned array should contain references to the same objects in the passed array. If the passed array is null, copy should return null.

Generality v. Capability
Generality v. Capability

Some of you might be wondering: why would I ever write a method that accepts an Object? It seems like you lose so much information about the object by doing that!

While that is true, the tradeoff is with generality. Let’s try and make that clear through an analogy:

Practice: List Unique Items

Created By: Geoffrey Challen
/ Version: 2021.9.0

Create a class Unique that provides a single class method uniqueItems. uniqueItems accepts a list of Objects and returns a count of how many of the objects in the list are unique, meaning that there are no other objects in the list that are equal to them. No items in the list will be null.

For example, given the list {1, 2, 4} you would return 3, whereas given the list {2, 2, 5} you would return 1. The list may contain any kind of Object. You may import collections like Maps or Sets for this problem, but they are not required.

Homework: Fix Locations

Created By: Geoffrey Challen
/ Version: 2022.9.0

It's easy to get mixed up between latitude and longitude! Happily, at least sometimes, this is an easy fix to correct.

Complete a public class LocationFixer that provides a single class method named fixLocation. fixLocation takes a Location object as its only parameter. assert that the passed value is not null. Some of those location objects have their latitude and longitude swapped. If that's the case, correct them! Otherwise, leave them unaltered.

You may be wondering: how will I know which locations are incorrect? The hint is that these are all locations from around the University of Illinois. That should help you determine when the latitude and longitude have been swapped. Note that negative latitudes represent the southern hemisphere, while negative longitudes represent the western hemisphere.

The Location object has getters and setters for double latitude and longitude values following our usual conventions: getLongitude, setLatitude, and similar. Note that the Location class is already defined and can be used without an import.

CS People: Barbara Liskov
CS People: Barbara Liskov

Only a few women have won the Turing Award, computing’s highest honor. Barbara Liskov is one of them, cited for her “contributions to practical and theoretical foundations of programming language and system design, especially related to data abstraction, fault tolerance, and distributed computing.”

Barbara Liskov is also a particularly appropriate person to learn about now, as we study polymorphism. She’s responsible for the informal rule that a subtype (or descendant) should behave like the supertype (the parent or ancestor) when using the supertype methods. But why don’t we let her explain it herself!

More Practice

Need more practice? Head over to the practice page.