+ 1

Java Challenge. Explanation 2

#1 Does constructor return any value? (+) Yes ( ) No This is a little confusing. I thought constructors do not return a value - it is operator 'new' that does. It returns a reference to a newly created instance of the class. Is that instance considered to be a return value constructor returns? #2 Which of these can be used to differentiate two or more methods having the same name? (+) Number of parameters ( ) Parameters data type ( ) None of these What about parameters data type? Suppose we have 2 methods: - boolean isTrue (int a, int b); - boolean isTrue (double a, double b); #3 Java allows to create a method which has the name as the class. - If I create such a NON-STATIC method and specify 'void' return type for it, the method can be accessed from a static context (e.g. main function) - If I create such a NON-STATIC method and specify 'int' return type for it, then I try to call it from a main method, compilation shows an error How to explain such a behaviour? #4 https://code.sololearn.com/crY69h5Ll2L6/#java Question 1. When do we practically use it? Question 2. Suppose another class D implements interace A as well and overrides its methods. Let's create A d = new D(); How does compiler know, which method implementation should be executed (the one in B or the one in D class)? I thought it depends on a data type of the variable that stores a reference. But here the data type is the same for both cases. #5 https://code.sololearn.com/c06e4Z9zk18w/#java Why the method with a String argument gets executed? #6 https://code.sololearn.com/cglH9JWxldgF/#java Why the output is false? Should we use equals() method with strings only? Suppose we have 2 static Integer objects defined in the same class and initialized with the same value. https://code.sololearn.com/cUc53zVkkGWH/#java Calling this method on these objects returns true. What is the explanation for this?

2nd Sep 2018, 12:13 PM
Steppenwolf
Steppenwolf - avatar
7 Réponses
+ 3
#1 Yes, the newly created object is what the constructor returns. #2 I agree with you, the datat type can also decide which method is called. #3 You cannot call a non-static method in a static context no matter the return type. However, you can invoke non-static method on an object in a static method, no matter the return type. #4 Suppose you have interface Animal and want to store object of classes Dog and Cat implementing it in one list. Then an item in the list is defined with type Animal. When you invoke a method, the executed code depends on the actual type of the object (not on the type of variable), which is either B or D. #5 To execute the one with object argument, try casting null to Object. #6 Class A doesn't have its own implementation of equals(), therefore the default, which return this==otherObj, is executed. And that's false, since you have two different objects.
2nd Sep 2018, 12:26 PM
michal
+ 3
Yes, methods and fields have different behavior, because fields cannot be overriden (unlike methods). The class B contains two variables called x (one inherited and one own) and the own variable hides the inherited. As a result, when you access the variable x with type B, you get the own variable (value 2) and when you access it with type A, you get the inherited variable (value 1).
2nd Sep 2018, 2:26 PM
michal
+ 2
Questions 2 is wrong, 1 is unclear. You can report them by tapping the 3 dots -> Report. You shouldn't treat null as a string, but as the more specific of the possibilities. String extends Object so it is more specific. If you create two methods with same name, one taking type X and one type Y and nor X neither Y is superclass of the other (like String and Integer) and pass a null to the method it throws compile error.
2nd Sep 2018, 1:45 PM
michal
+ 2
michal Wow, awesome. Still #4... https://code.sololearn.com/cgj40LtnuMBc/#java You mentioned: When you invoke a method, the executed code depends on the actual type of the object (not on the type of variable), which is either B or D. It is applicable for methods, but not applicable for variables. True?
2nd Sep 2018, 2:08 PM
Steppenwolf
Steppenwolf - avatar
+ 2
michal Thank you for your response!
2nd Sep 2018, 2:43 PM
Steppenwolf
Steppenwolf - avatar
+ 1
michal #1 Object is not actually a value... I find this question inaccurate then. As well as the second. So strange, that no one gets rid of them. #4 But when I try to access an attribute, it depends on the type of a variable, right? #5 Alright. But should I always regard null as string then?
2nd Sep 2018, 1:01 PM
Steppenwolf
Steppenwolf - avatar
+ 1
Steppenwolf You're welcome.
2nd Sep 2018, 4:10 PM
michal