Follow us on Facebook

Header Ads

Welcome to JAVA POint

Difference Between Method Overloading and overriding in Java(in java)

                     Difference Between Method Overloading and overriding in Java

                                Overloading Vs Overriding in JAVA


1: Over-burdening occurs at order time while Overriding occurs at runtime: The limiting of over-burden strategy call to its definition has occurs at accumulate time anyway restricting of superseded technique call to its definition occurs at runtime. 


2: Static techniques can be over-burden which implies a class can have more than one static strategy for same name. Static techniques can't be superseded, regardless of whether you proclaim an equivalent static strategy in youngster class it has nothing to do with a similar technique for parent class. 


3: The most fundamental contrast is that over-burdening is being done in a similar class while for superseding base and youngster classes are required. Superseding is tied in with giving a particular execution to the acquired technique for parent class. 


4: Static restricting is being utilized for over-burden techniques and dynamic restricting is being utilized for abrogated/superseding strategies. 


5: Execution: Overloading gives better execution contrasted with abrogating. The explanation is that the limiting of abrogated techniques is being done at runtime. 


6: private and last techniques can be over-burden yet they can't be superseded. It implies a class can have more than one private/last strategies for same name yet a youngster class can't supersede the private/last techniques for their base class. 


7: Return kind of strategy doesn't make any difference if there should arise an occurrence of technique over-burdening, it very well may be same or extraordinary. Anyway if there should arise an occurrence of strategy abrogating the superseding technique can have more explicit return type (allude this). 


8: Contention rundown ought to appear as something else while doing strategy over-burdening. Contention rundown ought to be same in technique Overriding.



Overloading Example:

/A class for adding upto 5 numbers 


class Sum 

int add(int n1, int n2) 

return n1+n2; 

int add(int n1, int n2, int n3) 

return n1+n2+n3; 

int add(int n1, int n2, int n3, int n4) 

return n1+n2+n3+n4; 

int add(int n1, int n2, int n3, int n4, int n5) 

return n1+n2+n3+n4+n5; 

public static void main(String args[]) 

Total obj = new Sum(); 

System.out.println("Sum of two numbers: "+obj.add(20, 21)); 

System.out.println("Sum of three numbers: "+obj.add(20, 21, 22)); 

System.out.println("Sum of four numbers: "+obj.add(20, 21, 22, 23)); 

System.out.println("Sum of five numbers: "+obj.add(20, 21, 22, 23, 24)); 


Output: 

Amount of two numbers: 41 

Amount of three numbers: 63 

Amount of four numbers: 86 

Amount of five numbers: 110 

Here we have 4 renditions of same technique add. We are over-burdening the technique add() here. 


Superseding model 


bundle beginners

class CarClass 

public int speedLimit() 

bring 100 back; 

class Ford expands CarClass 

public int speedLimit() 

return 150; 

public static void main(String args[]) 

CarClass obj = new Ford(); 

int num= obj.speedLimit(); 

System.out.println("Speed Limit is: "+num); 

Output: 

Speed Limit is: 150 

Here speedLimit() strategy for class Ford is superseding the speedLimit() technique for class CarClass.


0 Comments:

Post a Comment

C language and C++ progaming