OVERLOADING :
In java it is possible to define two or more methods
within the same class that share the same name as long as their parameter
declaration (i.e. signature) are different which is called overloading.
Overloading is basically means that you use the same
name of function with different signatures.
Java run time environment will check the parameters
of a function at run time whenever that method is called by the object and bind
according to the signatures.
1: class CircleArea
2: {
3: private double getArea(double radius)
4: {
5: double area = 3.145 * radius * radius;
6: System.out.println("Area is : "+area);
7: return area;
8: }
9:
10:
11: private float getArea(float radius)
12: {
13: float area = 3.145f * radius * radius;
14: System.out.println("Area is : "+area);
15: return area;
16: }
17:
18: private int getArea(int radius)
19: {
20: int area = 3 * radius * radius;
21: System.out.println("Area is : "+area);
22: return area;
23: }
24:
25: public static void main(String[] args)
26: {
27: CircleArea cir = new CircleArea();
28: cir.getArea(25.1452); //call the first method which is double type
29: cir.getArea(25.1452f); //call the second method which is float type
30: cir.getArea(25); //call the int type method
31: }
32: }
Output :
In the above program same function getArea is implemented
three times with different signatures like in first we passed double parameter,
in second we passed float parameter and in last implementation we passed int
parameter and return types of these methods are also different.
These functions are called by java run time
environment only when we pass parameter according to the data type.
OVERRIDING :
Extending the method of a parent class by adding
more functionality to its child class method is known as overriding.
In method overriding the signature and name must be
same. The new method which is again implemented in the child class is called
overridden method.
Consider following example :
1: class A
2: {
3: public int operation(int a, int b)
4: {
5: int c = a + b;
6: return c;
7: }
8: }
9:
10: class B extends A
11: {
12: //same method as in A class but the implementation is different
13: public int operation(int a, int b)
14: {
15: int c = a - b;
16: return c;
17: }
18: }
In the above example you can easily understand that
the method in class B has same name and signature but the implementation is
different than A class.
Note :
You can only override methods in the child
class not in the same class.
Now consider a diagram by which you can easily
understand the difference between the two :
Therefore, it is clear that overloading is done by using same name
with different signatures in the same class whereas overriding is done with the
use of same name, signature and different implementation in the child class.
Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.
ReplyDeletepython Training in Bangalore | python Training in Bangalore
nice post.thank you.
ReplyDeletejavacodegeeks.net