The whole duck isn't enough

  • 4
  • Locked
This program implements a Duck class, and even creates two specific ducks (two objects). Create a couple more categories of animals similar to the Duck class, namely Cat and Dog. Figure out what the toString method should return in these classes and create pairs of each creature (meaning objects) and display them on the screen.
You can't complete this task, because you're not signed in.
Comments (9)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Level 19 , Bordeaux, France
26 March 2021, 10:07
Something bad is I created the new classes "properly" in the Class Tree, and it worked well when I ran the programm, but CodeGym didn't recognized it :/
Level 18 , Athens, Greece
20 April 2020, 09:17
Why we use the STATIC word when create clases Duck/Dog/Cat? I knew that we use Static without create object. Am I wrong? IJ don't allow me to create those classes with Static
Level 23 , Columbia, United States
18 July 2020, 03:17
Static is used when a class is created within the same class as the main method. You don't have to use static when creating a new class as long as it's not on the same class as the main method. Could be wrong but that's what I understand.
kar-fai chow
Level 8 , Hong Kong, Hong Kong
18 November 2020, 11:03
i don't understand, doesn't all classes have a main method? How can you create a class that is not in the same class as a main method?
Level 18 , Roosendaal, Netherlands
28 March 2020, 13:57
how does it invoke toString method, we didn't even call it.
Jakub M
Level 32 , Ostrava, Czech Republic
27 October 2020, 09:36
That's correct, but as will be mentioned in one of the future lessons: The toString() method is inherited from the Object class. So basically every class has this method by default (like e.g. constructor) and when you call System.out.println(xy) you are acutually calling toString() method. I've acutally found one of the clarification also in lvl 3 (Screen output once again). "The Object class's standard toString() method returns a string consisting of the class name and the object's memory address (in hexadecimal form)." So we basically "override" the default implementation of this method. But as well method overriding (which is not same as overloading) will be taught in future lessons (as your lvl is 18 i think you already know all of those things :)). So for others - it would be more logical if they show you something like this:
public static class Duck {
        public String toString() {
            return "Duck";
Then it is quite apparent that we "override" some method which was implicitly implemented somewhere (in Object class). Hope this will help.
Joseph Haynes
Level 1 , United States
7 January 2020, 04:21
Maybe I am over complicating it ( I usually do), but even after all the lessons and googling "toString" method to see what others say about it, I still don't think I really understand what it does or what it is for. Like I said, I am probably over-thinking it or something...
Level 11 , Pittsburgh, United States
28 July 2019, 20:46
This is a very good example to start using keyword commands in Intellij to speed up coding time. Maybe not so with true beginners in programming but as you get seasoned in the workplace, using Ctrl+c, Ctrl+v, and Ctrl+r (replace) will make making your "dates" a bit easier to achieve.
Muhammad Vahhaaj
Level 19 , Rawalpindi, Pakistan
11 June 2019, 15:16
Very good, the toString is a method in object class which is already imported to the program and toString is used for String representation of an object.