Exam sample, I wonder if I understood

The question arises:

Suppose you oare a type reference Objectthat points to an object of a type Athat contains a method fand a method toString. Both parameters toStringand fhave no parameters. show the operator that calls the method toStringand the operator that calls the method f.

- answer:

 f();
 toString();
+5
source share
2 answers

No, it is not. First of all, you are not using an instance oto call methods. Without specifying an instance, the compiler will force these methods to implicitly call this.

-, o.f(), f Object. , , o A.

Object o = new A();
String s = o.toString();
((A)o).f();

.

+12

, . f toString A, .

f toString A , A, , :

A myA = new A();  // Assuming the existence of a no-args constructor
myA.f();
myA.toString();

Object, f, A.

+1

All Articles