A method can be used, and the return type discarded. For example, for a method that returns, say, an integer, instead of calling it like this:
int x;
x = MyMethod(1, 2);
You can also call it like this:
MyMethod(1, 2);
That is, without using the return value. In such a case, if you were to have different methods with the same parameters but different return values, the compiler wouldn't know which version of the method to use.
The method call cannot be resolved by looking at the return type.
Example :
void a(){
}
int a(){
}
Method call
a();
cannot be resolved.
A method's return value can be assigned to a variable:x = myMethod(a, b);or combined in a more complicated expressions:result = a + b + myMethod1(c, d);HOWEVER, it is also possible that the return value is discarded; that is, that the method is invoked, but nothing is done with the return value:myMethod(a, b);In this case, if there are different versions of "myMethod()" that only differ in their return value, the compiler won't know which version to use.
The only similarity is that both constructor and function overloads are distinguished by their signature -- the number and type of their arguments. Functions differ in that they also have a return type, which is also part of the signature, whereas constructors have no return type, not even void.
YES only if: The return type in the child class is a sub-type of the return type of the parent class. Ex: public int getName() {} - Parent class method public String getName() {} - Child class method If we implement the above two methods in the class hierarchy we will get compilation errors. whereas if we try the below implementation it will work: public Object getName() {} - Parent class method public String getName() {} - Child class method This is because String is a sub-type of Object and hence it works. This is also called as Covariant Overriding. Note: This feature is available only from Java 1.5. Earlier versions of Java expect overriding methods to have exactly the same return type as the super class method.
No. void is not a data type. It is mandatory for all java methods to return something and if it is not going to return anything, we have to mark the method with a "void" return type to let the JVM know that it must not expect anything from the method.
A method that return a value should have a return statement. The method signature should indicate the type of return value. While in the case of a method that does not return a value should not have a return statement and in the signature, the return type is void. When using a method that doesn't return a value, a programmer can not get a value from that function, but instead, it can only change variable values and run other methods.
A method's return value can be assigned to a variable:x = myMethod(a, b);or combined in a more complicated expressions:result = a + b + myMethod1(c, d);HOWEVER, it is also possible that the return value is discarded; that is, that the method is invoked, but nothing is done with the return value:myMethod(a, b);In this case, if there are different versions of "myMethod()" that only differ in their return value, the compiler won't know which version to use.
The only similarity is that both constructor and function overloads are distinguished by their signature -- the number and type of their arguments. Functions differ in that they also have a return type, which is also part of the signature, whereas constructors have no return type, not even void.
YES only if: The return type in the child class is a sub-type of the return type of the parent class. Ex: public int getName() {} - Parent class method public String getName() {} - Child class method If we implement the above two methods in the class hierarchy we will get compilation errors. whereas if we try the below implementation it will work: public Object getName() {} - Parent class method public String getName() {} - Child class method This is because String is a sub-type of Object and hence it works. This is also called as Covariant Overriding. Note: This feature is available only from Java 1.5. Earlier versions of Java expect overriding methods to have exactly the same return type as the super class method.
The final and finally keywords have no impact on the return type of a method in Java.
My answer may not be the intent of the designer of overloading. But here are my understanding and the best use of it: If you have a group of methods that are: 1. functioning the same (return the same object type), and 2. they takes different parameters or arugments, you may overload that that group of methods with the same name. Please note that the word may. (You have the option, but do not have to) One example is the overloaded method ToString() Some of other possible overloading ways are: ToString(string fmt), ToString(int start, int count); In other computer language, one may have to code as ToFormattedString(), ToSubString(), instead of using the same method name.
default return type is : true
No. void is not a data type. It is mandatory for all java methods to return something and if it is not going to return anything, we have to mark the method with a "void" return type to let the JVM know that it must not expect anything from the method.
A method that return a value should have a return statement. The method signature should indicate the type of return value. While in the case of a method that does not return a value should not have a return statement and in the signature, the return type is void. When using a method that doesn't return a value, a programmer can not get a value from that function, but instead, it can only change variable values and run other methods.
A return type describes the type of data which is returned by a call to a method. They keyword void is used to describe a method which does not return any data.
* void - This is a return type - or, more correctly, specifies that a method has no return type.* protected - This is an access modifier. It says that the only classes that can call this method are subclasses and classes in the same package.* int - This is a value type (or return type).* main - This is the name of the method.
Overloading the same method name with different number of arguments (and the data types), and perhaps with a different returned data type. The method signatures are different, only the names are the same. Overriding is to change the same method name with different implementation (the method body). The method signature stays the same.
Yes. Any base class method that is declared virtual can be overridden by a derived class. Overriding a method that is not declared virtual can still be called, but will not be called polymorphically. That is, if you call the base class method, the base class method will execute, not the override. To call a non-virtual override you must call it explicitly.