InterviewSolution
| 1. |
Comment on method overloading and overriding by citing relevant examples. |
|
Answer» In JAVA, method overloading is made POSSIBLE by INTRODUCING different methods in the same class consisting of the same name. Still, all the functions differ in the number or type of parameters. It takes place inside a class and enhances program readability. The only difference in the return type of the method does not promote method overloading. The following example will furnish you with a clear PICTURE of it. class OverloadingHelp { public INT findarea (int l, int b) { int var1; var1 = l * b; return var1; } public int findarea (int l, int b, int h) { int var2; var2 = l * b * h; return var2; }}Both the functions have the same name but differ in the number of arguments. The first method calculates the area of the rectangle, whereas the second method calculates the area of a cuboid. Method overriding is the concept in which two methods having the same method signature are present in two different classes in which an inheritance relationship is present. A particular method implementation (already present in the base class) is possible for the derived class by using method overriding. Both class methods have the name walk and the same parameters, distance, and time. If the derived class method is called, then the base class method walk gets overridden by that of the derived class. |
|