Know About Overloading and Overriding Method in Java

method Overloading and Method Overriding

Hello coders! If I am not wrong, you all must be very busy learning different programming languages every day. So, today I am here with a very important topic in Java programming language. If you are the one who writes codes in Java, then this article is especially for you. In this article, I am going to give complete information that you need to know about method overloading and overriding in java. Find yourself in juggling through multiple assignments? Ask us to help you with your Java Programming Help

Method Overloading and Method Overriding in Java

Some of you might have already known about these methods in Java. And many of you are hearing about this for the first time. So, I suggest you read the article completely to know everything about method overloading and overriding in Java. Even if you know about them, you might become fluent if you revise them once again. So, let’s get started.

First of all, I am going to describe in detail the method of overloading. You will learn what method overloading is? where and how it is used. And also the advantages of using method overloading.

What is Method Overloading?

Method Overloading is simply defined as a class having multiple methods with the same name but different parameters. This method in java is known as method overloading. But when you try to perform only one operation that is having the same name as the methods, at that time this method increases the readability of the program.

Let us look at the meaning of the method overloading in other words. Suppose you across a class of a Java program that has a plural number of methods. The name of all those methods is the same name but their parameters are different with a change in type or number of arguments. You can use them to perform a similar forms of functions using those methods. This process is known as method overloading.

With the help of Overloading, you will be able to use different methods with the same name. But you need to remember that they have different signatures. And you can differentiate the signature by the number of input parameters or type of input parameters or sometimes both.

According to my research, I have come to know that Overloading is related to compile-time or static polymorphism. Because using method overloading you can implement Compile time or static polymorphism. This also helps to increase the readability of any program. Method overriding helps you provide the specific implementation of the method which you will already provide by its superclass earlier. You can perform Method overloading in a java program only within the class.

Let me tell you few pointers that you have to keep in mind while performing overloading methods in any type of program in Java.

Important Points to know while overloading in java

  • Remember that you cannot overload a return type. It is not possible in the method overloading technique.
  • However it is possible to overload static methods. But remember that the arguments or input parameters that you use in the program have to be different.
  • And it is not possible to overload two methods if they only differ by a static keyword.
  • You can overload the main() method in the program just like other static methods.

To understand this concept better, I will take an example using which you will be able to perform multiplication of given numbers. You can use any number of arguments a user can put to multiply. You have to see from the user’s point of view where the user can multiply two numbers or three numbers or four numbers or so on. For multiplying two values you need to write the method such as multi(int, int) with two parameters. And for multiplying three values multi(int, int, int) with three parameters, and so on.

The technique method-overloading is used by a programmer to figure out the program quickly and efficiently. You need to apply this Method Overloading technique in a program when you observe that the objects in the program are required to perform similar tasks but don’t forget to do this under different input parameters.

When a method is called by an object in your program, the compiler will first start matching up the method name in the code you have written. Later it matches the number and type of parameters to decide what definitions it should execute. In this way, multiple tasks will be assigned to the same method which is known as Polymorphism.

Types of Method Overloading in Java

Now, I will show you the different ways of method overloading in Java. The following are the methods you need to know:

Type-1:

This is based on the number of parameters in the program. In this type, the entire overloading of the methods completely depends on the number of parameters that you will place within the parenthesis of the method.

Let me show you an example of this type:

import java.io.*;

class AdditionExample {

public int add(int x, int y)

{

int sum = x + y;

return sum;

}

public int add(int x, int y, int z)

{

int sum = x + y + z;

return sum;

}

}

class Parametertype1 {

public static void main(String[] args)

{

AdditionExample ob = new AdditionExample ();

int sum1 = ob.add(1, 2);

System.out.println(“sum of the two integer value :”

+ sum1);

int sum2 = ob.add(1, 2, 3);

System.out.println(

“sum of the three integer value :” + sum2);

}

}

Look at the above example carefully so that you will understand this type of method overloading clearly.

Type-2:

This type of method overloading is based on the data type of the parameter given in the program. The overloading of the method takes place with the arrangement of data type, and within the parameter.

Now,  look at the following example of this type:

import java.io.*;

class AdditionExample {

public int add(int x, int y, int z)

{

int sum = x + y + z;

return sum;

}

public double add(double x, double y, double z)

{

double sum = x + y + z;

return sum;

}

}

class Parametertype2 {

public static void main(String[] args)

{

AdditionExample ob = new AdditionExample ();

int sum2 = ob.add(1, 2, 3);

System.out.println(

“sum of the three integer value :” + sum2);

double sum3 = ob.add(1.0, 2.0, 3.0);

System.out.println(“sum of the three double value :”

+ sum3);

}

}

Look at the above example carefully so that you will understand the second type of method overloading clearly. Now, we shall look at the last type in this method overloading.

Type-3:

This type of method overloading is based on the sequence of data types in the parameters of your java program. In this type, you will come to know that the method overloading also depends on the ordering of data types of parameters within the method.

The following is an example of this type:

import java.io.*;

class Parameter {

public void methodoverloading(String name, int id)

{

System.out.println(“parameterdata :” + name + ” “

+ “Id :” + id);

}

class parametertype3 {

public static void main(String[] args)

{

Parameter parameter = new Parameter ();

parameter.parameterdata(“Arun”, 1);

parameter. parameterdata(2, “Bharath”);

}

}

This is all about the method overloading and its types. I hope, now you got clear information about method overloading. Now, I will start describing about method overriding.

What is Method Overriding in Java?

When you write a program in Java, sometimes a subclass or child class has the same method as the parent class. At this time method overriding takes place within your program. In other words, when a subclass provides a particular implementation of a method declared by one of its parent classes, at this method overriding takes place.

You will experience this feature or method specifically when you implement a method already given in one of its super-classes, or parent classes, in any object-oriented programming language.

You can say that the process of redefining a parent class’s method in a subclass is also known as method overriding. As the compiler doesn’t really know the type of object passed on the compilation, this process is known as run time polymorphism or dynamic binding.

You can use this method of overriding whenever a class has several child classes Suppose, if a child class needs to use the parent class method, it can use it and the other classes. But you can use this only when you want to use a different implementation. Using this overriding feature, you can make changes without touching the parent class code.

When it comes to benefits of method overriding, you can use this method for the implementation of runtime or dynamic polymorphism in Java. Using method overriding, you can also provide a specific implementation or definition of a method in a class. But remember that the class should be already in an existence in its superclass.

It is also used to define what behavior a class can have and how that behavior will be implemented by the class that will inherits it.

Just like method overloading, the method overriding is also divided into various types. But, this method has only two types whereas the method o  overloading has three types. Now, look at the types of method overriding in Java.

Type-1:

This type of method overriding uses Handle Access-Modifiers in a Java program. In this method, you will observe that the increase in activities of the overriding’s method will give more access than the overridden method.

The following is an example of this type:

class BaseClass{

   protected void disp()

   {

       System.out.println(“Parent method”);

   }

}

class ChildClass extends BaseClass{

   public void disp(){

      System.out.println(“Child method”);

   }

   public static void main( String args[]) {

      ChildClass obj = new ChildClass();

      obj.disp();

   }

}

Type-2:

This type of method overloading is based on the  Super keyword in Method Overriding. In this method, you will use the super keyword to call the parent class method or class constructor.

In the following example, you will be able to under the super keyword in method overriding clearly.

Example:

class Methodoverloading{

   public void method()

   {

System.out.println(“Overridden “);

   }    

}

class Demo extends Methodoverloading{

   public void Method(){

super.Method();

System.out.println(“Overriding “);

   }

   public static void main( String args[]) {

Demo obj = new Demo();

obj.Method();

   }

}

So, with this, you might get an idea about what is the method overriding in Java. Therefore, I conclude this is all about the method overloading and overriding in Java. There are many uses of these methods in Java. So, I suggest you must learn these methods.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top