Which of the following Statement Is Legal Declaration and Definition of a Method

Methods are distinguished by the compiler by the number and type of arguments passed to the method. Therefore, draw(String s) and draw(int i) are different and unique methods. You cannot declare multiple methods with the same signature: draw(String s) and draw(String t) are identical and result in a compiler error. Note that overloaded methods must return the same data type. Therefore, void draw(String s) and int draw(String t) declared in the same class produce a compile error. A method with the same signature and return type as a method in a parent class overrides or hides the parent class method. The Method Override and Masking Methods section describes what each means, how to override and hide methods, and explains related issues. Methods can return primitive data type values or reference data types. The isEmpty() method of the Stack class returns a primitive data type, a Boolean value: The Java programming language supports name overloading for methods, which means that multiple methods of the same class can use the same name if they have different parameter lists. For example, suppose you have a class that can draw different types of data (strings, integers, and so on) and contains a method for drawing each data type. In other languages, you must find a new name for each method, such as drawString, drawInteger, drawFloat, and so on. In the Java language, you can use the same name for all drawing methods, but you can pass a different type of argument to each method.

Therefore, the data drawing class can declare three methods named draw, each accepting a different type of argument. The data type of the value returned by the return statement must match the type of data that the method should return. You cannot return an object from a declared method to return an integer. When returning an object, the data type of the returned object must be a subclass of or exactly the specified class. When returning an interface type, the returned object must implement the specified interface. While this may sound like writing a novel, rather than simply declaring a method for a class, most method attributes can be declared implicitly. The only two mandatory elements of a method declaration are the name of the method and the type of data returned by the method. For example, the following declarations are a method named isEmpty() in the Stack class that returns a Boolean value (true or false): Which of the following methods is/are legal method declarations? Options A and C are wrong because public and protected are less restrictive. Options B and D are incorrect because abstract and synchronized are not access modifiers. What is the most restrictive access modifier that allows members of one class to access members of another class in the same package? The default access modifier is the packet-oriented access modifier. To fix the problem and have option B compile, you need to add an additional pair of braces: “Your comments will only be displayed after manual approval.

The last three links in the list cover topics that warrant their own lesson or have already been included in another lesson. Selecting these links will take you to another part of this tutorial. Be sure to come back!.