Hi guys. I am trying to get a handle on understanding the passing of primitive and reference data type in to functions. It seems to me that passing primitive data into a function is analogous to passing a reference data. I have written some code to help me explain myself.
Basically, I created a Persons class which contains, among other things, a redefinition of the toString method that was inherited from the Object class. In main, I have two functions, function1(double) and function2(Object).
When int number is passed into function1, I understand number is implicitly promoted to a double to match the signature of function1. Is it right to say that the same effect happens when a derived object Persons is passed into function2? An alternative way of looking it is that since Persons extends Object, every Persons instance is an instance of Object.
Remove the cast. You don't need it, since all objects have toString(). And remember that polymorphism means you get the correct overriding version for the runtime type of the object.
You would have to check the Java® Language Specification, but as far as I can remember, there is no “promotion” of reference types.
. . . since Persons extends Object, every Persons instance is an instance of Object.
Correct, but call the class Person (singular) not Persons. Reserve plural forms for utility classes.
posted 1 week ago
Thanks Campbell Ritchie! You're absolutely right. Not only the cast is not needed, it's wrong to have it in the definition of saySomething(Object). Suppose another object of type different than that of Persons is passed in, my original definition would call the toString() method of Persons every time, which is not the desired result.
I picked up learning Java a few weeks ago and have been part of this site for the same amount of time. In this short period of time, I've gained valuable information, particularly from you. Thanks very much again!
ken luong wrote:Suppose another object of type different than that of Persons is passed in, my original definition would call the toString() method of Persons every time, which is not the desired result.
No, you would get a ClassCastException. The Java runtime will always check the actual type and if it finds that the object cannot be cast to Persons, it will throw the exception.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
"No, you would get a ClassCastException. The Java runtime will always check the actual type and if it finds that the object cannot be cast to Persons, it will throw the exception."
You are right Junilu Lacar. Any object that isn't of a class which is derived from Persons would cause a ClassCastException to be thrown. I hope I thought it through this time😁. Thank you for your correction!
posted 1 week ago
ken luong wrote:Thanks Campbell Ritchie! . . .
That's a pleasure
my original definition would call the toString() method of Persons every time . . .
More likely, you will be unable to complete the cast, and the program will fail with an exception.
Remember that toString() is an instance method and is therefore implicitly subject to polymorphism. [Only instance methods can be polymorphic.] The runtime starts looking for a definition of the method in the Class<?> object corresponding to the Object you are using. If there is no such method definition, it goes to the superclass and keeps going until it finds such a method. Since it is declared in Object, it “knows” it will eventually find such a method.
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad: