What happens if you keep a method name same as the class name and the method has a return type?
Ngày đăng:
18/09/2022
Trả lời:
0
Lượt xem:
13
Problem Set To ensure that you understand all the material in this lecture, please solve the the announced problems after you read the lecture. If you get stumped on any problem, go back and read the relevant part of the lecture. If you still have questions, please get help from the Instructor, a CA, or any other student. Show
Can we have a method name same as class name?Yes, It is allowed to define a method with the same name as that of a class. There is no compile-time or runtime error will occur.
Can we have same method name with different return type?No, you cannot overload a method based on different return type but same argument type and number in java. same name.
Can you have two methods with the same name?Two or more methods can have the same name inside the same class if they accept different arguments. This feature is known as method overloading. Method overloading is achieved by either: changing the number of arguments.
When a method uses a class name as its return type?When a method uses a class name as its return type, the class of the type of the returned object must be either a subclass of, or the exact class of, the return type. Suppose that you have a class hierarchy in which ImaginaryNumber is a subclass of java.
|