Java Overloading and Overriding: Best Practices and Examples

Method overloading

In a class you can write more than one method with the same name by changing the parameters as follows: --------- i.e. 

No of parameters
Types of parameters
Order of parameters

OR

In java it is possible to define two or more methods within the same class that can share the same name but different parameter declaration, in this case we can say method is overloaded, and the process is called as method overloading.

Method over loading is one of the ways that implements polymorphism

Note:
Return types don’t play a role in overloaded resolution.

Method overloading support polymorphism because it is one way that 
Java implements the “one interface, multiple methods paradigm.

Ways to Implement Overloading

  1. Overloading methods
  2. Overloading constructors 


Explanation:

test () is overloaded four times. the first version takes no parameter the second takes one integer parameter and the third takes two integer parameters and the fourth takes one double parameter 


Method invoking depends on parameter.

When you overriding methods, return type is not a matter i.e. you can use any return type which will not be used in method invocation.

Constructor overloading: -----writing multiple constructors in a class by changing the parameter is called constructor overloading.

Return type: ------------no need.


METHOD OVERRIDING

Implementing the super class method in subclass with the same name and with the same signature. This is called as method overriding.  

Rules for method overriding: --------

Subclass method name must be same as super class method name.
Subclass parameters must be same as super class parameter.
Subclass method return type must be same as super class method return type.
Subclass method access specifier must be same as super class method access specifier or higher than super class method specifier           

Super class         Subclass
private                private, default, protected, public 
Default               Default, protected, public
Protected           Protected, public   
public                  Public   

When super class method is throwing an exception in subclass, then you do one of following: --

You can omit the method level exception in subclass
You can use same method level exception in subclass
Sub class methods can throw subclasses to super class exception.
Subclass method can’t throw super class to super class exception class. 

Assume that super class has a method of follows: ---------

                      Void m1(int x)throws B

You can override this method with one of the following form: ----

                      Void m1(int x)[  protected void m1(int a)throws B]

                      Void m1(int x)[  public void m1(int a)throws B]



Chandra Sekhar

A Blogger and Author! This post was written and edited by me, a technologist. I started this site to share my inspirations, work, and free materials, which I hope others may find useful as well. Man with a creative streak who enjoys experimenting with various web design strategies.

Previous Post Next Post

Microservice Communication in a Distributed System