Difference Between Overloading and Overriding a Java Method

Overloading means having two methods in the same class with the same name but different parameters or whereas Overriding occurs when parent class and child class have a method with same name and parameters.

Overloading means having two methods in the same class with the same name but different parameters or whereas Overriding occurs when parent class and child class have a method with same name and parameters.
The main importance of overriding is that it allows child class to have its own implementation of a method found in the parent class.


Overloading Vs Overriding

  • Overloading happens at compile-time wheres Overriding happens at run-time.
  • Polymorphism does not apply in Overloading, only in overriding.


Example of Overriding 

/**
 *
 * @author Eric
 * www.techoverload.net
 */
public class Birds {
    void haveWings()
    {
    System.out.println("All birds have Wings. ");
    }
    
    
}

//Inner Class Penguins
class Penguins extends Birds
{
    //Overriding method haveWings which has been declare in parent class
void haveWings()
{
System.out.println("Penguins have wings to enable them "
        + "move in water easily.");

}
public static void main(String args[])
{
new Penguins().haveWings();//Create a new Object Penguins and calling haveWings method.
}
}

Explanation

Above class Penguins Inherits haveWings() method from its superclass or parent class.Penguins class goes ahead to do its own implementation on this method.Read more about Inheritance

Example of Overloading

/**
 *
 * @author Eric
 * www.techoverload.net
 */
public class SumNumbers {
  static   void calculateTotal(int a,int b,int c)
    {
    int total=a+b+c;
    System.out.println("Total is " +total);
    }
    
  static   void calculateTotal(int a,int b)
    {
    int total=a+b;
    System.out.println("Total is " +total);
    }
    public static void main(String args [])
    {
   
    calculateTotal(123,23,67);
      calculateTotal(98,34);
    }
    
}

Quick Lessons to learn from this Tutorial.

Here are some of the lessons you can learn from above code.

1.There are two ways of overloading methods.These are 


             Changing the number of parameters.

             Changing Data types of parameters.

2.A non-static method cannot be referenced inside a static context.

That the reason why calculateTotal method is static.

3.When calling a static method you don't have to create a class object.

That why we have not create SumNumbers object.


Above overloading, examples use numbers of parameters as the way to overload calculateTotal method.

COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Difference Between Overloading and Overriding a Java Method
Difference Between Overloading and Overriding a Java Method
Overloading means having two methods in the same class with the same name but different parameters or whereas Overriding occurs when parent class and child class have a method with same name and parameters.
Java Codes
http://www.download-all.net/2017/10/difference-between-overloading-and.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/10/difference-between-overloading-and.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy