Difference between method overloading and method overriding in java

method overloading and overriding

Before knowing the difference between method overloading and overriding let’s understand the terms, What is method overloading and What is method overriding in java.

What is method ?

before  knowing method overloading let’s understand what is method. “Methods are basically collection of statements that are used to defined  some specific  tasks” Methods are basically collection of statements . Method allows us to reuse the code when same type of solution we required. Like in the below example method is defined to performs the addition operation so when we need to add the 2 number then again an again we call this method.

example:-

int addition(int firstNumber, int secondNumber){     //function addition is of int return type which take 2 input.

int addi = firstNumber + secondNumber ;         //Addition of 2 number performs.

return addi ;                                                               // return the value of the addition where the function is called.

}

What is method overloading.

When any class have more than one methods having a same name but different parameters then it’s called the method overloading.

example:-

 

class Main{

static int addNumber( int a , int b ) {
return a + b;

}

static double addNumber( double a , double b) {
return a + b ;

}

public static void main (String[] args) {

int myNum = addNumber( 45 , 98 ) ;

double myNum1 = addNumber( 67.3 , 3.43) ;

System.out.println(“int :” + myNum);

System.out.println(“double :” + myNum1);

}
}

 

What is method overriding .

If a child class has the same method as declared in its Base class ( parents class ), then it is knows as method overriding.

Condition for  Method Overriding in java.

  • method should be have same name as the base class (parents class).
  • method should be have same parameter as the base class..
  • method should be have parents child relationship (Inheritance).

example :-

 

             class Mobile {
void show()
{
System.out.println(” Mobile is an Parent class of any brand mobile “);
}
}

class Iphone extends Mobile {                          // Inherited class

void show()
{
System.out.println(“Parent class of the Iphone is Mobile”);
}
}

class MainClass {
public static void main(String[] args)
{

Mobile m1 = new Mobile();
m1.show();

Mobile  m2 = new Iphone();
m2.show();
}
}

Now we know What is method overloading and What is method overriding now we are ready to learn the difference .

Difference between method overloading and overriding .

S.NO METHOD OVERLOADING METHOD OVERRIDING
1. It is an compile time Polymorphism It is an  run time Polymorphism.
2. It increase the readability of the program.  It is used to provide the specific implementation of the method which is already present in it’s super class.
3. Method overloading  is occurs in the same class While Method Overriding is performed in two class which in the parent class relationship (Inheritance).
4. Method overloading doesn’t required inheritance. While it always required  inheritance.
5. In this, methods must have same name and different signature.

In method overloading method should be have same name but different arguments.

While in this, methods must have same name and same signature.

While in method overriding methods should be have same name and same argument

6. It can or can’t be have same return type. While it must be have same or co-variant return type.

 

After reading this article i hope now you have good understanding over What is method overloading, What is method overriding , and  Difference between method overloading and overriding.

If you think this article is useful to you then please comment down your thoughts.

if you want to know about:-

What is java and the best way to learn java. chick here.                                                                                                                   What is core java and advance java. click here                                                                                                                                   OPPs Concepts. click here

Leave a Reply

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

Reading is essential for those who seek to rise above the ordinary.

Discover Abloggerdude

Welcome to Abloggerdude, an author oriented theme.
A place where words matter. Discover without further ado our countless community stories.

Build great relations

Explore all the content form Abloggerdude community network. Forums, Groups, Members, Posts, Social Wall and many more. You can never get tired of it!

Become a member

Get unlimited access to the best articles on Abloggerdude and support our  lovely authors.

© 2020, Abloggerdude
Skip to toolbar