Point class.PointC@ and then a number that has nothing to do with x or y.+, Java calls the toString() method on that object to get the String to concatenate. So here we are seeing that objects created from record constructors have a different toString() than those created with classes.PointC print like PointR? Do I write a toString?equals()? And by default record puts in a good toString?equals() and toString() coming from?Object (with a capital O). It has the “default” implementations of toString and equals that all classes use if they don’t define their own. If they do define their own, it is called overriding that method.Object does is effectively default methods. But it’s actually using a slightly different mechanism than implements – it is using inheritance, which has some of its own tradeoffs. I’ll give a small example where I make up my own class called Objekt with a stringify() method. The idea is to show the structure that Java has with its own built-in Object, but writing it all out ourselves:stringify on sc2 without having it defined at all. I guess that means it is using the one on Objekt?SubClass1 definition says @Override. What’s that? I tried deleting the whole @Override line and the program behaved the same.@Override says “I mean to override this method!” Java will only give an error if that method isn’t defined on a class or interface being extended or implemented.extends is another word here, what does that mean?Prof: When a class Child extends another class Parent, it tells Java that Child is a subclass of Parent (we call Parent the superclass). This means a few things happen:
- Method calls and field lookups on
Childobjects will look for methods and fields inParentif they aren’t found onChild Childobjects “count” as having theParenttype, so they can be stored inParentvariables and parameters, in elements ofParentarrays, and so on
This is a lot like interfaces, except for the fields piece, which we haven’t explored, but we can get to that soon.
PointC getting the defaults from Object?Prof: Another great question – if you don’t write extends on a class definition in Java, it implicitly extends Object. It’s like Java adds that in for you. So the default toString and equals really are coming from there!
Also there’s one more definition worth sharing now: Java says subtyping is what’s happening when one type can be stored in a variable of a different type. So in Java’s terms, SubClass1 in the example above is a subtype of Objekt because it can be stored in an Objekt-typed variable. Subtyping also applies to interfaces: Circle is a subtype of Pict because of the implements relationship.
Practice
Objekt example, which of these lines compile? Select all that do.Objekt o = new SubClass1();SubClass1extendsObjekt, so aSubClass1object can be stored in anObjektvariable.SubClass1 s = new Objekt();The storing only goes one way.SubClass1extendsObjekt, not the other way around, so anObjektcan’t be stored in aSubClass1variable. It doesn’t even have anxfield.SubClass1 s = new SubClass2();Both classes extendObjekt, but neither one extends the other, so aSubClass2can’t be stored in aSubClass1variable.Object o = new SubClass2();Objekthas noextends, so it implicitly extendsObject. ASubClass2counts as anObjekt, and anObjektcounts as anObject.
Objekt o = sc1;, which method body runs for o.stringify()?- The one in
SubClass1, so it returns"SubClass1[x=0]"The object stored inois still theSubClass1object, and its class overridesstringify. It works the same way as callingdrawon aPictvariable that holds aCircle. - The one in
Objekt, so it returns"Objekt"The variable’s type isObjekt, but the object in it is aSubClass1, and the object’s class decides whichstringifyruns. - Neither, because the line doesn’t compile
Objekthas astringifymethod, so Java allows the call on anObjektvariable.
toString that makes PointC print the way PointR does.double x;
double y;
PointC(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return ;
}
}
String s = "" + new PointC(4.0, 5.0);