0

I want to know what is happenening in these statements

Sample obj1 = new Sample(); Sample obj2; obj2 = obj1.makeTwice(); makeTwice is a public method in Sample class I want to know, will obj2 point at class Sample as a whole or particularly makeTwice method in Sample class? What is exactly happening with variable obj2?

24th Apr 2020, 11:31 PM
Sagar Gupta
Sagar Gupta - avatar
3 ответов
0
Sagar Gupta No. A object can only call methods from its class or parent class. A dog can not meow and a cat can not bark. It is a law of life and of Java as well. So obj1 can only call methods in the Sample class or of a parent class. obj2 however is just a variable with type Sample. It can not call any methods of any class because it is not an object of any class.
25th Apr 2020, 12:56 AM
Ore
Ore - avatar
0
The returned value of the makeTwice function is assigned to obj2. If it the function returns null, obj 2 is null.
25th Apr 2020, 12:26 AM
Ore
Ore - avatar
0
Ore Adeleye can a reference variable call methods of its own class and other class as well? Like when we assign that variable with other class reference so will it be able to class its own class methods as well even when its not pointing at its own class?
25th Apr 2020, 12:49 AM
Sagar Gupta
Sagar Gupta - avatar