Using the feed classes, whose method body runs for each call on
Repost r = new Repost("Free pizza in the CSE lobby", "tritons");?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)
r.preview() runs the one in r.mentions("pizza") runs the one in r.toString() runs the one in With
Post p = new VideoPost("My cat learns Java", 42);, which of these are true? Select all that are.p instanceof VideoPostThe object’s own class isVideoPost.p instanceof MessageVideoPostextendsMessage, one step up the diagram.p instanceof PostMessageimplementsPost, so the search finds it going up.p instanceof ObjectEvery class ends up atObject, at the top of the diagram.p instanceof RepostRepostis a sibling ofVideoPost, not above it, so the search going up never reaches it.
This BusStop leaves out super(lat, lng). What happens?
class Location {
double lat;
double lng;
Location(double lat, double lng) {
this.lat = lat;
this.lng = lng;
}
}
class BusStop extends Location {
int route;
BusStop(int route, double lat, double lng) {
this.route = route;
}
}- It doesn’t compileA subclass constructor has to run a
Locationconstructor first. Without asuper(...), Java triesLocation()with no arguments, and there isn’t one. - It compiles, and
latandlngstart at0.0Java never leaves inherited fields unset because a constructor skipped them. It requires aLocationconstructor to run, and this one has nosuper(...). - It compiles, and Java passes
latandlngtoLocationby itselfJava doesn’t pass arguments along on its own. Every subclass constructor in this chapter wrotesuper(...)for that reason.
Finish
Location’s equals: two Locations are equal when their lat and lng match, and nothing else equals a Location.class Location {
double lat;
double lng;
Location(double lat, double lng) {
this.lat = lat;
this.lng = lng;
}
@Override
public boolean equals(Object other) {
if (this == other) { return true; }
if () { return false; }
Location l = ;
return lat == l.lat && lng == l.lng;
}
}
Location geisel = new Location(32.8811, -117.2376);
boolean same = geisel.equals(new Location(32.8811, -117.2376));
boolean notString = geisel.equals("Geisel Library");
boolean different = geisel.equals(new Location(32.7341, -117.1446));
double lat;
double lng;
Location(double lat, double lng) {
this.lat = lat;
this.lng = lng;
}
@Override
public boolean equals(Object other) {
if (this == other) { return true; }
if () { return false; }
Location l = ;
return lat == l.lat && lng == l.lng;
}
}
Location geisel = new Location(32.8811, -117.2376);
boolean same = geisel.equals(new Location(32.8811, -117.2376));
boolean notString = geisel.equals("Geisel Library");
boolean different = geisel.equals(new Location(32.7341, -117.1446));