Thankfully able to pass this task, but I would still like some clarifications.
1. @Override - which class is the toString method overriding belonging to?
and
2. - how can the toString method called upon itself when there are no other lines within the program to call to this method?
3. Also, when passing in an object, all of the objects' characteristics (String name for example) is passed on to the methods correct? So when defining this.mother = mother or this.father = father we're actually saying the object we just passed as parameters is referring to the object we created in the class?
Thank you for any insights!
Just two questions
Under discussion
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Mike McKenna
7 November 2019, 22:04
Hi,
I believe that GG answered your questions far better than I could .
Hope i helped
+1
Guadalupe Gagnon
7 November 2019, 21:00
1. toString() is a method of the Object class. Because all classes inherit from the Object class they all have a default toString method.
2. the println() method takes as an argument 'Object'(s). Because toString() is defined in the Object class, it can call the toString() method on everything passed to it, which it does automatically without the programmer needing to specify it. Primitives are the only types that don't inherit from Object . Here is a quick lesson on primitives: https://en.wikibooks.org/wiki/Java_Programming/Primitive_Types
3. I think you are specifically talking about the println() method. The println() method only works with what it can infer from the Object class, so it won't specifically know all the information from your custom class. However, through polymorphism (a principal or OOP), when toString() is called it will specifically call the toString() that is defined in the class that you are passing to the println() method. Polymorphism is really a great functionality and I have solved quite a few codegym tasks by creating my own custom classes and overriding the toString() method.
+2
Jimmy Phan
8 November 2019, 00:07
Thank you for taking your time and answering my questions thoroughly.
So what you're saying is that the toString() method is automatically called every time we attempt to print from an object, but we do not see it. For this task, we override this toString()method when we call the println()method so that we can manually print the requirements "...is the cat's name. ... is the cat's father and ... is the cat's mother", am I correct?
I think my third question was more for when passing Objects to the constructor's parameters. For example: Cat catDad = new Cat(dadName, catGrandma, catGrandpa). Do the program recognizes that we're passing catGrandma & catGrandpa objects and set Cat.grandma as catGrandma and set Cat.grandpa as catGrandpa?
0
Guadalupe Gagnon
8 November 2019, 03:36useful
with regards to the constructor: When you add arguments to the constructor it will match those arguments with the parameters of the constructor being used inside the class. It is important to know that the variable names that you use have 0 meaning to the computer and are only for helping humans understand. The thing that the computer understands is parameter types. Here is an example:
Because these constructors have the same exact parameter types, this code would be illegal. You could argue that name and address are completely different things but to the computer both of these constructors (int, String), the variable names don't mean a thing above that. So in your example the only way that you could get the constructor to set the proper fields of grandpa and grandma is by passing in the arguments in the exact way that matches a constructor. So with your example above (dadname, catGrandMa, catGrandpa) is the constructor looked like this:
The grandma and grandpa cats would be backwards in that cat object because that is the way you passed them into the constructor. The computer would not know the difference. +1
Guadalupe Gagnon
8 November 2019, 03:38useful
and regards to the println() method and toString():
Yes. If you commented out the code for the overridden toString() method and ran the code you would get the default toString() of the Object class being called and it will just print a jumble string 8 or 10 digits long that represented the spot in the computers memory where that object is stored.
+1
Jimmy Phan
8 November 2019, 16:07
Beautifully explained. I'll need to re-read the constructor explanation again. Thanks a ton man
0
Guadalupe Gagnon
8 November 2019, 16:27useful
If you haven't noticed yet constructors are basically methods. They both serve different purposes but are designed in the same fashion. As you go along with learning you will find a lot of similarities between seemingly different concepts. Once you start making those connections learning speeds up considerably.
+1