201k views
4 votes
When one method calls another, which of the following statements are true?I. The class methods and instance methods of one class may call the public class methods of another class using dot notation and referencing the name of the other class.II.The class methods and instance methods of one class may call the public instance methods of another class by using dot notation to invoke the method on an instance of the other class.III.The instance methods of a class may call, only using dot notation, any class method of the same class.

A)I onlyB)III onlyC)I and II onlyD)I and III onlyE)I, II, and III

2 Answers

5 votes

Answer:

(c) I and II only

Step-by-step explanation:

Class methods of a class are also called static methods of that class in Java. They are methods that are bound to the class itself rather than objects of the class.

Instance methods on the other hand are bound to the objects of the class.

(I) is true. In Java, and many other programming languages like Python, class methods and instance methods may call public class methods of another class by using the dot notation and referencing the name of the other class.

For example, given:

Class A, with class method cAMethod(), and an instance method iAMethod() which are both public (i.e have access modifier of public which makes them visible to all other classes in the same package) and;

Class B, with class method cBMethod(), and an instance method iBMethod().

cAMethod() and iAMethod() methods of class A can call the class method cBMethod() of class B as follows;

B.cBMethod()

===========================================================

(II) is true. The class methods and instance methods of one class may call the public instance methods of another class by using dot notation to invoke the method on an instance of the other class.

For example, given the same classes as above;

Let's assume an instance of class B is b.

cAMethod() and iAMethod() methods of class A can call the instance method iBMethod() of class B as follows;

b.iBMethod()

===============================================================

(III) is false. Instance methods of a class may call the class methods of that same class with or without dot notations.

For example, given the same classes as before;

iAMethod() instance method of class A can call the class method cAMethod() of same class A directly without dot notation as follows;

cAMethod()

or with the name of the class and a dot notation as follows;

A.cAMethod()

User Christian Burgos
by
3.4k points
4 votes

Answer:

C) I and II only

Step-by-step explanation:

To call a method of one class from another class, we need to instantiate the other class inside the main class. An instance of one class can invoke the behavior of other instances, whether or not it belongs to its own class. The requirement is that the invoking instance has a reference to the invoked instance. Instance methods belong to the object of the class and not the class itself.

The following syntax can be followed:

ClassName object = new ClassName();

object.method();

User Tom McLean
by
3.7k points