Function overloading is an interesting feature of object oriented programming. Function overloading is one type of polymorphism. We can define number of functions with same name but the argument list of the functions must be different. The return type of overloaded functions may be same or different. The body statements of the overloaded functions will be definitely different. Function overloading can be done in the same class or in sub classes.
How overloaded functions are executed
When any function calling statement is executed, compiler checks the name of the function first and then the argument list. The binding between the function calling statement and the function definition is done accordingly.
Utility of function overloading
Function overloading reduces complexity of a program. Functions with same name but different body statement means when these overloaded functions are being executed different actions are performed. For example take the case of ‘indexOf()’ function of String class. This function has four overloaded versions like ‘int indexOf(char ch)’, ‘int indexOf(char ch, int i)’ ‘int indexOf(String s)’ and ‘int indexOf(String s, int n)’. The first function takes a character as argument and returns the first occurance location of the character in the invoking string object. The second function returns the occurance of the argument character after the location specified by ‘int i’. The third and fourth function perform similar jobs as stated above but on argument strings.All the functions returns -1 if the character of string is not present in the invoking string object.
Constructor overloading is another important feature where via execution of constructors objects are created but different argument constructors will create objects differently. A simple example will demonstrate this feature perfectly.
class A
{
int a,b;
A()
{
}
A(int x, int y)
{
a=x;
b=y;
}
}
The first constructor is without any argument which means it creates the object with default values on the data members. The second constructor sets two values on object data members while creating the object.
Sir can u please tell me why the throws IOException statement is used in BufferedReader input but not in Scanner input?
ReplyDeleteAlso what are the advantages of try-catch block over throws?
Please Sir reply soon, our practicals are on the 11th!!
throws IOException is required for 'readLine()' function of BufferedReader class as the function is defined that it may throw IOException object. For Scanner class, there is no such functions.
Deletethrows statement throws the generated Exception object to the calling statement, it does not take care of the object but try...catch absorbs the generated Exception object and handles it. This is the advantage of try..catch over throws.
Thank you sooo much!!! :)
DeleteWelcome.
DeleteI have to pass in my annual xams of computer.. ...
ReplyDeleteI had already cleared my basics.. ...but not at an advanced level for programming... ...
I had An argument with my computer teacher .....
And he took a grudge on me and as a result he failed me in my pre annual xams For no reason .....
I am damn sure That he will fail me in the upcoming annual xams.. ........
I request you to suggest me a Java programming book for isc board class 11.....only for programming.. ......
Please it's urgent... ...........