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)
interface Post { ... } class Message implements Post { ... } class Repost extends Message { ... } class VideoPost extends Message { ... }BothRepostandVideoPosthave solid arrows toMessage, and the one dashed arrow goes fromMessagetoPost.interface Post { ... } class Message implements Post { ... } class Repost extends Message { ... } class VideoPost extends Repost { ... }HereVideoPostextendsRepost, so its arrow would point toRepost, making a chain of three classes. In the diagram,RepostandVideoPostboth point toMessage.interface Post { ... } class Message { ... } class Repost extends Message implements Post { ... } class VideoPost extends Message implements Post { ... }Here the dashed arrows toPostwould start atRepostandVideoPost. In the diagram, the only dashed arrow starts atMessage.interface Post { ... } class Message extends Post { ... } class Repost extends Message { ... } class VideoPost extends Message { ... }A class can’textendsan interface. Java stops withno interface expected here. The dashed arrow meansimplements.
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()
interface Place { ... } class Location implements Place { ... } class Park extends Location { ... } class DogPark extends Park { ... }Each arrow goes from a subclass up to its superclass:DogParktoPark, andParktoLocation.interface Place { ... } class Location implements Place { ... } class Park extends Location { ... } class DogPark extends Location { ... }HereDogParkwould point toLocation, side by side withPark. In the diagram,DogPark’s arrow ends atPark.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 listLocationanyway: extendingParkalready brings in everythingParkgets fromLocation.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, soDogPark, whose arrow ends atPark, 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()
interface Place { ... } interface Rated { ... } class Location implements Place { ... } class Restaurant extends Location implements Rated { ... }A class extends one class but can implement interfaces too:Restauranthas a solid arrow toLocationand a dashed one toRated.interface Place { ... } interface Rated { ... } class Location implements Place, Rated { ... } class Restaurant extends Location { ... }Here the dashed arrow toRatedwould start atLocation, not atRestaurant.interface Place { ... } interface Rated { ... } class Location implements Place { ... } class Restaurant extends Location, Rated { ... }Ratedis an interface, so it goes afterimplements, notextends. Andextendstakes only one class.interface Place { ... } interface Rated { ... } class Location implements Place { ... } class Restaurant implements Location, Rated { ... }Locationis a class, soRestauranthas toextendsit. Java stops withinterface expected here.