I've got a class (named Implement), with one sub-class (Bicycle).
I have several different types of variable which I would like to print, however I am having difficulty printing a String + Int + Boolean.
Is it possible to print all of these in a single statement?
You need to override toSrting() method in your Bicycle class.
If you do, you will be able to write:
The result was:
*Vehicle Statistics* Seats: 1, Name: Haro BMX, Contains Moving Parts: true, Materials: Steel, Rubber Plastic
Although, I had to correct your code because it didn't compile. The order of constructor parameters was different in the code in your first post compared to the code in your second post.
Also, you can't split a String literal between lines.
Also, I missed Implement class so I needed to create one.
Please, always provide a SSCCE when asking questions.
It will be easier for others to help you.
Cory Marcus wrote:I actually have overridden toString in the Bicycle class, although I may be off in my method.
No. That sounds fine.
The main problem I see is that the only thing that's defined in Bicycle is 'seatNumber'. Are the other fields defined in Implement? (not a great name for your superclass, BTW)
My suggestion would be that you show us the code for that class too; but off the top of my head, I'd say that at the very least you're missing a super(...) call in your constructor.
So it actually seems like I have to make an object array of the sub-class (Bicycle).
I then add two objects to this.. and loop the array and print what each object is constructed from.
This sounds thoroughly confusing to me.
I'll also post the rest of my code, to make more sense. Apologies, I should've just done so originally.
You still have lines too long; I have broken up the worst offenders.
You may have been warned against the + operator on Strings because of performance problems. That warning is correct, but you can use the + operator as many times as you like on Strings and get fast performance as long as all the + operators are in the same expression. I think that you have the String divided into parts which are compile‑time constants, so the javac tool will do the String joining for you rather than leaving it to runtime. The best way to join multiple Strings all in the same expression is what beginners all use: the + operator.
Back to inheritance. I am not sure that Implement is the best name for the superclass, but that is a minor question. Remember that a subclass IS‑A superclass and a subclass instance IS‑A superclass instance. Those things are by no means easy to achieve. You have correctly put three of the four fields in the superclass and correctly provided public getXXX methods. Those methods are implicltly inherited by subclasses. But you should give all the fields (in both classes) private access. You have correctly set up the fields in the constructors. But you should give each class only one constructor. But you should not be able to access private members. You have forgotten the super() syntax. If you read the Java® Language Specification, you get the full instance creation process, which is probably difficult to read, but it progresses something like this:-
Start Bicycle constructor, but do nothing.
Go to superclass (=Implement) and call constructor, but do nothing.
Go to Object class constructor. Remembering that Object has no superclasses, execute all code in that constructor.
Back to Implement constructor. Execute all its code.
Back to Bicycle constructor. Execute all its code.
Notice you have two problems:
Remember the super(...); call must be the first thing written in the constructor. Now the superclass constructor sets up three of the fields, the subclass constructor sets up the fourth, and you can delete lines 35-37. And it will all work nicely.
I am sure other people will be able to produce much better suggestions than mine.
You can get the words Implement and Bicycle in the toString method (or any other non‑static method) like this:-
String.format("...%s...", ..., getClass().getSimpleName(), ...)
Because of polymorphism you do not have to change that in subclasses. Try it.
Campbell Ritchie wrote:You would have to replace the field names with getXXX method names in the overridden toString method, but there may be a simpler way to do it:-That is a very crude attempt which you will need to refine for pretty‑printing.
Obviously, great minds think alike because I was about to post something just like that.
@Cory: The only minor issue I'd take with Campbell's post is that toString() is NOT meant for "pretty printing". It is
"a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read."
(from the Object.toString() docs)
and I seem to remember reading somewhere (probably EJ) that it should be written with debugging in mind.