☰ Contents Extending Classes

Navigating Class Hierarchies

Student: So can a class extend a class that extends another class?
Prof: Indeed.
Student: And can two classes extend the same class?
Prof: Yup.
Student: And can one class extend two other classes?
Prof: No! That’s actually an interesting case called multiple inheritance, which Java doesn’t support. I welcome you to look it up on your own, though.
Student: Hm. OK. How do you keep it all straight?
Prof: Class diagrams can help. We draw classes and interfaces with arrows between them. An arrow between two classes points from the subclass to the superclass. A dashed arrow points from a class to an implemented interface. We try to lay out the picture so Object goes on top, and all the arrows point up. Here’s an example using Location:
interface Place { ... }
class Location implements Place { ... }
class Park extends Location { ... }
class BusStop extends Location { ... }
class Restaurant extends Location { ... }
Object
toString()
equals(Object o)
Location
double lat
double lng
pinLabel()
onScreen()
BusStop
int route
↑ pinLabel()
Restaurant
double stars
↑ pinLabel()
Park
int acres
↑ pinLabel()
interface
Place
pinLabel()
onScreen()
Student: What’s the ↑?
Prof: That’s a marker for a method that is overriding a method on a superclass.
Student: Ah. Why isn’t onScreen() in the classes like BusStop?
Prof: They don’t have their own implementation: they inherit it from Location. So if you have an object of type BusStop, Java needs to look around in the class hierarchy to find the method for it.
Student: Oh interesting, so does the hierarchy go in a memory diagram?
Prof: Kind of. It’s fixed when we write the program, because we can’t change where an arrow points when the program is running. It sort of sits off to the side and helps us figure out method and field lookups.

Practice

Which code matches this class diagram?
Object
toString()
equals(Object o)
Message
String message
preview()
mentions(String word)
Repost
String from
↑ preview()
VideoPost
int seconds
↑ preview()
interface
Post
preview()
mentions(String word)
  1. interface Post { ... }
    class Message implements Post { ... }
    class Repost extends Message { ... }
    class VideoPost extends Message { ... }
    Both Repost and VideoPost have solid arrows to Message, and the one dashed arrow goes from Message to Post.
  2. interface Post { ... }
    class Message implements Post { ... }
    class Repost extends Message { ... }
    class VideoPost extends Repost { ... }
    Here VideoPost extends Repost, so its arrow would point to Repost, making a chain of three classes. In the diagram, Repost and VideoPost both point to Message.
  3. interface Post { ... }
    class Message { ... }
    class Repost extends Message implements Post { ... }
    class VideoPost extends Message implements Post { ... }
    Here the dashed arrows to Post would start at Repost and VideoPost. In the diagram, the only dashed arrow starts at Message.
  4. interface Post { ... }
    class Message extends Post { ... }
    class Repost extends Message { ... }
    class VideoPost extends Message { ... }
    A class can’t extends an interface. Java stops with no interface expected here. The dashed arrow means implements.
Which code matches this class diagram?
Object
toString()
equals(Object o)
Location
double lat
double lng
pinLabel()
onScreen()
Park
String name
int acres
↑ pinLabel()
DogPark
boolean offLeash
↑ pinLabel()
interface
Place
pinLabel()
onScreen()
  1. interface Place { ... }
    class Location implements Place { ... }
    class Park extends Location { ... }
    class DogPark extends Park { ... }
    Each arrow goes from a subclass up to its superclass: DogPark to Park, and Park to Location.
  2. interface Place { ... }
    class Location implements Place { ... }
    class Park extends Location { ... }
    class DogPark extends Location { ... }
    Here DogPark would point to Location, side by side with Park. In the diagram, DogPark’s arrow ends at Park.
  3. interface Place { ... }
    class Location implements Place { ... }
    class Park extends Location { ... }
    class DogPark extends Park, Location { ... }
    A class extends only one class in Java (no multiple inheritance). It doesn’t need to list Location anyway: extending Park already brings in everything Park gets from Location.
  4. interface Place { ... }
    class Location implements Place { ... }
    class DogPark extends Location { ... }
    class Park extends DogPark { ... }
    This turns the arrows around. An arrow points from the subclass to the superclass, so DogPark, whose arrow ends at Park, is the subclass.
Which code matches this class diagram?
Object
toString()
equals(Object o)
Location
double lat
double lng
pinLabel()
onScreen()
Restaurant
String name
double stars
↑ pinLabel()
stars()
interface
Place
pinLabel()
onScreen()
interface
Rated
stars()
  1. interface Place { ... }
    interface Rated { ... }
    class Location implements Place { ... }
    class Restaurant extends Location implements Rated { ... }
    A class extends one class but can implement interfaces too: Restaurant has a solid arrow to Location and a dashed one to Rated.
  2. interface Place { ... }
    interface Rated { ... }
    class Location implements Place, Rated { ... }
    class Restaurant extends Location { ... }
    Here the dashed arrow to Rated would start at Location, not at Restaurant.
  3. interface Place { ... }
    interface Rated { ... }
    class Location implements Place { ... }
    class Restaurant extends Location, Rated { ... }
    Rated is an interface, so it goes after implements, not extends. And extends takes only one class.
  4. interface Place { ... }
    interface Rated { ... }
    class Location implements Place { ... }
    class Restaurant implements Location, Rated { ... }
    Location is a class, so Restaurant has to extends it. Java stops with interface expected here.