Methods in Java: Overloading, Recursion, Static methods and the final keyword

We have already seen the basics of methods , the way it is created and the various ways it can be used in our programs. In this tutorial, we shall see other ways in which the methods can be used to meet the programmer’s desire.

Method Overloading

Method overloading refers to the creation of more than one methods with the same name in the same class. Method overloading is a way that demonstrates polymorphism(or more accurately static polymorphism) in Java. This flexibility of the methods comes with one restriction. The methods that share the same name must not have the same parameter. They must differ in this aspect to preserve the identity of the method.

The methods can either have a different number of parameters, or different data type of parameters or can have a sequential change in parameters with different data types. The compiler must know which method is being called. Also, the methods can have different return types but it is not enough for the compiler to distinguish between the methods. Here is an example of method overloading:

Unable to retrieve the Code part.
Please reload again.
Notify us if the problem still persists.
Till we work on this you can view code on URL below.
Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/methods/MethodOverloading.java
Output:-
The value is : 5
The value is : B
The value is : 56.9
The values are : C , 45

Recursion

Recursion is the process in which a function calls itself and the method that calls itself is known as a recursive function. This means that the method call statement is present in the body of the method itself.

When using recursion, it is important to put a terminating condition just as in for loop to pass the control back to the calling method. If there’s no terminating condition then the function will run repeatedly just like an infinite loop. It is also important to note that recursion is an important application of stack. Here is an example of a recursive program:

Unable to retrieve the Code part.
Please reload again.
Notify us if the problem still persists.
Till we work on this you can view code on URL below.
Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/methods/RecursionDemo.java
Output:-
Enter the range :
4
8
The factorial of 4 = 24
The factorial of 5 = 120
The factorial of 6 = 720
The factorial of 7 = 5040
The factorial of 8 = 40320

Static methods

The methods that have the static keyword in their method definition are known as static methods. They add special utility to methods, such as a static method can be called by another static method directly, i.e. without the creation of an object.

And a static method is accessed by the compiler first. This is the reason why we declare the main() method as static. But using static comes with the restriction that it cannot make use of this or super keyword in any way. Here is an example of a static method :

Unable to retrieve the Code part.
Please reload again.
Notify us if the problem still persists.
Till we work on this you can view code on URL below.
Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/methods/StaticMethodDemo.java
Output:-
Inside the static method meth()

Final Keyword

The final keyword is a non-access specifier which is used to prevent the value or the content of the classes, fields, and methods declared as final to be changed. It has different purposes in different contexts.

  • A variable declared as final: A variable declared as final will have its value fixed. According to the convention, the fields declared as final are usually written in capitals using an underscore to separate words. Local variables can also be declared as final.
  • A method declared as final: A method declared as final cannot be overridden. Eg Object class which has a lot of final methods which are not meant to be overwritten.
  • A class declared as final: A class that is declared as final cannot be inherited by another class. It is useful when creating immutable(cannot be extended) classes. Eg: String class.

A field that is declared as final behaves as a constant and must be initialized at the time of declaration either by directly assigning the value or by assigning the value with the help of a constructor(in the case of variables).

The examples of the declaration of a final class, method, and variable are shown below:

/**
  * This is a final class.
  */
  public final class ClassName {
     final int YES = 1;  //This is a final variable.
     final int NO = 0;  //This is a final variable.
   
     //This is a final method.
     public final void getResponse() {
        //body
     }

So, that’s all for this tutorial. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Do come back for more because learning paves way for a better understanding.

Keep Coding!! Happy Coding!! 🙂

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index