Overloading vs Overriding in Java

Overloading and overriding are completely different. Only the notion about interface (function) name is same. Overloading is the ability to use same interface name but with different arguments. Purpose of functions might be same but the way they work will differ based on the argument types. Overriding is applicable in the context of inheritance. When there is a need to change the behavior of a particular inherited method, it will be overridden in the sub-class.

Overloading itself is not polymorphism and similarly run-time binding itself is not polymorphism. All these and more are used to exercise the object oriented property polymorphism.

Overloading and Overriding Key Points

  • private: A java private method cannot be overridden because, it is not accessible to an inheriting class.
  • final: Method overloading a Java final method is possible but overriding a final method is not allowed.
  • final: Two methods with same parameters list and in one method we have parameters ‘final’, in this case two methods are not different methods and overloading is not possible.
  • static: Method overloading a Java static method is possible but overriding a static method is not possible.
  • static: Overriding is in the context of objects (instances) and inheritance. A method declared as static belongs to the whole class (common for all objects). So there is no point of overriding it in sub-class.
  • static: In overloading if two methods are different only by a static modifier then its not possible to overload. The arguments list must be different.

Overloading

Let’s discuss overloading in detail. In websites, I generally see a overloaded truck as example for overloading. It symbolizes that, adding more attributes, methods to a class and making it look bulkier is overloading. Please get it right, in fact when we use overloading for the outsiders view the class will look compact.

Before Overloading

I have shown two images, first one is called Harmonium. Its a classical music instrument from India and its a primitive version of piano. The other is a modern digital keyboard. In our overloading context, Harmonium is before overloading and keyboard is after overloading.

After Overloading

This is an attempt to closely mimic overloading :-)

Java Example for Method Overloading


public class OverloadExample {
	
	public static void main(String args[]) {
		System.out.println(playMusic("C sharp","D sharp"));
		System.out.println(playMusic("C","D flat","E flat"));
	}

	public static String playMusic(String c, String d) {
		return c+d;
	}
	public static String playMusic(String c, String d, String e){
		return c+d+e;
	}
}

Java Overloading Puzzle

package com.javapapers.java;

public class NullArgumentOverloading {

	public static void main(String[] args) {

		NullArgumentOverloading obj = new NullArgumentOverloading();
		obj.overLoad(null);
	}

	private void overLoad(Object o) {
		System.out.println("Object o argument method.");
	}

	private void overLoad(double[] dArray) {
		System.out.println("double array argument method.");
	}

}

Can you guess the output?

Overriding

When a class is inherited we get the base class’ methods and attributes based on the respective access modifiers. A specific inherited method’s behavior may not be suitable in the sub-class. So we replace the inherited method’s behavior as required. After overriding, the old behavior is completely obsolete.

Look at this monster truck, the old small four wheels are replaced with huge wheels to suit the need. This is overriding in real world.

Overriding

Overriding in Java / JDK

Implicitly every object in Java is extended from Object class. Object has a method named equals. In String class the default behavior is overridden.  The String equals behavior is modified to check if the character sequence is same as the compared one and returns true.

This Core Java tutorial was added on 03/10/2009.

Comments on "Overloading vs Overriding in Java" Tutorial:

  1. Suresh says:

    Nice comprehensive information sir….Thanks.

  2. chaitanya says:

    Nice example to understand the concepts easily Thanks

  3. Andy says:

    Nicest example that I have ever seen or think of for these blury concepts “overloading” and “overriding” in my 12 years exposure to java. Your simple way of explaining is superb! Hearty thanks & wishes!!

  4. veera says:

    very very thank u sir …very nice explanation sir

  5. dipankar says:

    thanks a lot for explanation.. only now I understand clearly the difference between overloading and overriding

  6. sujana says:

    This information is superb.Thanx alot.I understand this topic very easily by those examples. the summary of key points on how Java implements overloading and overriding is superb. Thanq

  7. Vishal says:

    A nice way to explain OL and OR.

  8. Muthukumar says:

    Awesome…

  9. Mitesh Agrawal says:

    Nice Observation on how Java does overriding. Thanks.

  10. Asim Nawaz says:

    Great explanation

  11. ps.krishna says:

    wow…simply superb

  12. mukesh says:

    thnx sir it is helpful for understanding overloading and overriding.
    thanks

  13. Anonymous says:

    overriding is explained well, overloading concept is also good but the use of word “interface” is quite confusing….

  14. RaamNaresh Reddy says:

    (it is Just Good For Beginers)..
    And…. It is not Sufficient…for advanced experts. overloading puzzle is good.
    Give Two three Programatical Examples…

  15. Zeeshi says:

    Can you give a code example for over riding like over loading….?

  16. Nitin says:

    Overloading simplest example

    public class OverLoad
    {
    void sum(int x, int y)
    {
    int sum=x+y;
    System.out.println(“sum is: “+sum);
    }
    void sum(int x, int y, int z)
    {
    int sum=x+y+z;
    System.out.println(“sum is: “+sum);
    }
    public static void main(String arg[])
    {
    OverLoad ol=new OverLoad();
    ol.sum(5,4);
    ol.sum(5,4,3);
    }
    }

    o/p-> sum is: 9
    sum is: 12

    Overriding simplest example

    class A
    {
    void calculate(int x, int y)
    {
    int sum=x+y;
    System.out.println(“sum is: “+sum);
    }
    }
    public class B extends A
    {
    void calculate(int x, int y)
    {
    int product=x*y;
    System.out.println(“product is: “+product);
    }
    public static void main(String arg[])
    {
    B b=new B();
    b.calculate(5,4);
    }
    }

    Run this code o/p will be 20

    class A
    {
    void calculate(int x, int y)
    {
    int sum=x+y;
    System.out.println(“sum is: “+sum);
    }
    }
    public class B extends A
    {
    /*void calculate(int x, int y)
    {
    int product=x*y;
    System.out.println(“product is: “+product);
    }*/
    public static void main(String arg[])
    {
    B b=new B();
    b.calculate(5,4);
    }
    }

    run this code o/p will be 9

    now i think u are clear… if still not. leave the comment

  17. Mukesh kumar says:

    your concept is very clear please send me some concept and example in my email related to applet,exception ,thread… Thanks

  18. Abhiram says:

    Joseph , I could not digest “Run time binding is not polymorphism”.But I believe it is otherwise. Overloading is static binding since at compile time it is bind,And also overloading is not polymorphism”, whereas overriding is polymorphism since it is binded at runtime.

    Clear the confusion

  19. anand says:

    overloadin=it is the relation biw the method declare in supper class and subclass having thr same method name name diffirent no input and diffirent type of input–;;; for eg
    public void m1()// no argument constructer
    public void m1(int i)// argumenter contructer
    public void m1(inti,String s)// this is all about the method overloading

  20. Raja Ramesh says:

    Nice Explanation for Overriding. The truck example for overriding is cool.

  21. Kiran says:

    Dear Joseph, this is the very simple and easy to understand article.so we are thank to you………………

  22. Bharati says:

    If example is given for overriding then it would be possible to understand more clearly….

  23. Nitin says:

    example is given above in my comments

  24. Praveen says:

    Is it true that Overloading is not a polymorphism?

  25. chandru says:

    this overloading and overriding concept is very clearly explained,and before reading this topic i didn’t understand these two task difference,now am understand completely. thank to you

  26. usharani pasala says:

    nice sir…

  27. Ram Gowtham says:

    Hi joe!
    So gud!

  28. raji says:

    All your answers are awesome relating to realtime easy understanding thank you for sharing your knowlegde. can you please explain java polymorphism it hasnt been very clear to me all time although partially i understand still not clear.

    thanks
    raj

  29. Steve says:

    Nice fundamental introduction on overloading and overriding. A deep analysis would be appreciated.

  30. Anonymous says:

    Hi,

    In one of the interview I got this question:

    Consider this example:

    class P {

    public void abc() {

    }

    Now class C inherits class P

    class C extends class P{

    public void abc() {

    // adds some additional functionality;

    }
    Now, here is the question:
    Due to some reasons if you want the functionality of abc method of class P (i.e parents )in child’s class, then how we can achieve this?. Is it through key word super?

    I understand this is very strange, but it was asked in interview

    Thank u,

  31. keerthi says:

    Give an example when you can not overload a method in Java.

  32. dennise says:

    excellent Java tutorial and simple thanks

  33. sree says:

    wow simply super…..great explanation .thank u

  34. Archu says:

    hi its really useful 4 me to understand tanqu…

  35. Deepesh says:

    Consider this example:

    class P {

    public void abc() {

    }

    Now class C inherits class P

    class C extends class P{

    public void abc() {

    // adds some additional functionality;

    }
    Now, here is the question:
    Due to some reasons if you want the functionality of abc method of class P (i.e parents )in child’s class, then how we can achieve this?. Is it through key word super?

    I did not understand the question here..
    Can’t we do like this –

    P sup = new P();
    P sub = new C();
    sup.abc(); //Calls super class method
    sub.abc(); //Calls sub class method

    Please let me know if its not correct..

    Thanks,
    Deepesh

  36. thnx sir it is helpful for understanding all concepts. Iam a beginner,I want to download all your core concepts for java, How Can i download, please send to my mail.

    Thanxs a lot, great job!!!

  37. K S Ravikumar says:

    Thanks for explaining in a simple language.

  38. Gokul says:

    Good Work :)

  39. Kannaaa says:

    hi jo..thank you so much…its very useful website…

    Karunakar

  40. Java Learner says:

    There are times when your article is complete and one requries no more reading.

    this is NOT one of them. Please try to put some examples and rules/protocol associated.
    eg: you didnt explain the access modifiers / scope in over-riding
    eg: return type in overloading

    gud effort nevertheless…

  41. Anonymous says:

    Thankyou so much..!

  42. Anonymous says:

    Never read a better article elsewhere. Simple description and I will never forget overloading and overriding hereafter.

  43. shilpishilpisrivastav says:

    excellent !
    can u give some more example in c++

    Shilpi

  44. Noel says:

    Amazing way of drilling the concept of Overloading and Overriding into the head.

    Thanks Joe

  45. pranav says:

    can return type differ in overloading and overriding?

  46. mubashir says:

    Nice one, Thanks…..

  47. seshu dheeraj says:

    nice explanation but little bit confusion can u please explain it clearly…….

  48. Ajit says:

    Really good and practical

  49. Anonymous says:

    best tutorial

  50. Sam says:

    @Nitin:Good one…:-)

  51. Anonymous says:

    very nice example with real world…:)

  52. Anonymous says:

    Thank you sir :) i got to know the concept

  53. pallavi says:

    can you override the static.

  54. puneet verma says:

    i really thanks to you very much ……i understood both overloading and overriding concept very well.

  55. Raju says:

    Nitin given this example to explain overriding but i have a doubt in this below example that is “What is the use of extending A class in B?” without extending that class also we will get the same result na…

    Overriding simplest example

    class A
    {
    void calculate(int x, int y)
    {
    int sum=x+y;
    System.out.println(“sum is: “+sum);
    }
    }
    public class B extends A
    {
    void calculate(int x, int y)
    {
    int product=x*y;
    System.out.println(“product is: “+product);
    }
    public static void main(String arg[])
    {
    B b=new B();
    b.calculate(5,4);
    }
    }

    Run this code o/p will be 20

  56. vikas says:

    Overriding simplest example

    class A
    {
    void calculate(int x, int y)
    {
    int sum=x+y;
    System.out.println(“sum is: “+sum);
    }
    }
    public class B extends A
    {
    void calculate(int x, int y)
    {
    int product=x*y;
    System.out.println(“product is: “+product);
    }
    public static void main(String arg[])
    {
    B b=new B();
    b.calculate(5,4);
    }
    }

    Run this code o/p will be 20

  57. Anonymous says:

    Superb its very useful thing which u have explained in detail with a very gud examples …Thanks a lot

  58. Anonymous says:

    Thank you sir : )

  59. Abhishek says:

    I like your way of explanation.
    Its good and easy to understand.

  60. Nadeesha says:

    Thank you for this nice explanations!

  61. Ruksana Shaikh says:

    Good one.

  62. anjani says:

    hi joe i have seen couple of your articles which were very intereseting and simple … thanks for sharing your knowledge.

  63. Anonymous says:

    nice answer

  64. Anonymous says:

    thank you sir

  65. Pritesh says:

    very nice article
    There is any concept of consructor overriding?

  66. Anonymous says:

    very nice explanation sir….
    u have explained it very well….

  67. Trilochan Chetri says:

    thanks for giving a clear view in real time application….

  68. Bhargava says:

    Nitin given this example to explain overriding but i(raju) have a doubt in this below example that is “What is the use of extending A class in B?” without extending that class also we will get the same result na… (raju……)

    Overriding simplest example

    class A
    {
    void calculate(int x, int y)
    {
    int sum=x+y;
    System.out.println(“sum is: “+sum);
    }
    }
    public class B extends A
    {
    void calculate(int x, int y)
    {
    int product=x*y;
    System.out.println(“product is: “+product);
    }
    public static void main(String arg[])
    {
    B b=new B();
    b.calculate(5,4);
    }
    }

    Run this code o/p will be 20

    Raju on April 26th, 2012 10:53 pm
    ——————————–
    Raju, without extending class A also possible, but if u want to access method(s) in class A, u need to create object for class A seperately.
    If we write class B extends A
    there is no need of creating objects for both classes. simply we can create object for class B and also access method(s) in class A also. That is behaviour of inheritence.
    class to class – extends
    interface to interface – extends
    class to interface- implements

  69. rahmath says:

    good explanations… it helps to learn easily. thank you to everyone.

  70. Vitthal Shirke says:

    Nice explanation!

  71. fresher says:

    good

  72. Anonymous says:

    good Example Sir
    thanks

  73. yogini says:

    hii,
    these concepts r too easy to understand

  74. sundar says:

    ok sir, i understood your concept but i want
    to get run time input with super key word code with explanation.

  75. prashant says:

    thnx i understand this very easily………

  76. jignesh & ajay says:

    Thanks…
    It’s my pleasure…

  77. dileep says:

    Simple and more understandable..

  78. […] have two classes B and C inheriting from A. Assume that B and C are overriding an inherited method and they provide their own implementation. Now D inherits from both B and C […]

  79. surekha says:

    hai sir this is good i able to understood this and thank u so much

  80. aisha elahi says:

    thnk u so much sir

  81. Krishna says:

    An Excellent explanation and helps a lot to the person who is new to java….very good explanation to all the articles…I am following the posts regularly….Thank you so much….

  82. Fareed says:

    Excellent !.

  83. Arjuncc says:

    Wonderful

  84. prasad n says:

    Excellent explanation thank you sir.and i have one doubt what is static overloading and dynamic overloading .give me one example

  85. Ankit Singh says:

    best comparison i have evry found on net is this one….. cleared all my doubts. thx for this. (first time in my life i am writting a comment on some blog in last 7 years)

  86. Lavanya says:

    great explanation.. Helped me understand the terms better. Thank u..

  87. Pushpraj singh says:

    nice example with explain…

  88. Anonymous says:

    very good nitin

  89. riya says:

    nice example……it’s easy to understand explanation

  90. Rakesh Gujrathi says:

    I clear my concept about overriding & overloading but I want practical example on both where I can programiticaly use it.

  91. Ramya says:

    Simply Super

  92. Raj Chaudhary says:

    Very good example about both.

    Thanks

    Raj Chaudhary

  93. Anonymous says:

    Statement : Derived classes still can override the overloaded methods. Compiler will not binding the method calls since it is overloaded, because it might be overridden now or in the future.

    Can anyone help me understand this statement?

  94. Sudhakar says:

    Hello sir your way of explanation is very good.
    Thank you very much sir.

  95. Anonymous says:

    the method is good for the st udents but to put briefly in the libriry system.

  96. sree latha says:

    hi,

    joe am a bigneer i need your help please let me know were do i get good understandable programs to practice and simultaneously complete java topics to learn.reply me as soon mas possible urgent……………………….

  97. rajkumar says:

    thank you sir,
    it is very helpful me

  98. SB says:

    NICE

  99. ANIRBAN says:

    the best example i’ve seen… thanks a lot sir.

  100. kamlesh says:

    simply superb…

  101. Geethapriya says:

    its very interesting and easy to understand…Thanks a lot…

  102. kathu says:

    great sir…..thank u…

  103. […] to the standard console and a newline. There are multiple println methods with different arguments (overloading). Every println makes a call to print method and adds a newline. print calls write() and the story […]

  104. Anonymous says:

    very very very niceee………………

  105. […] use it in all your classes. The class which you want to be cloned should implement clone method and overwrite it. It should provide its own meaning for copy or to the least it should invoke the super.clone(). […]

  106. Anonymous says:

    The example of harmonium and piano has explained the term Overloading in awesome way…
    Cant forget this term ever in my life from now…

  107. Rashmi Mishra says:

    the example explained is awesome, cant ever forget this term in my life.

  108. Anonymous says:

    very very nice example

    by
    devaraj.k

  109. Anonymous says:

    i have one question regarding overloading, can we achieve overloading in case of inheritance.

    As explained in below example is overloading is achieved in class B.

    class A
    {
    public void test(int i)
    {
    System.out.println(“From class A test method”);
    }

    }

    class B extends A
    {

    public void test(int i,int j)

    {
    System.out.println(“From class B method”);

    }

    public static void main(String args[])
    {

    }

    }

  110. Anonymous says:

    nice

  111. dakshayani says:

    thank u sir

  112. […] and write a custom excel view. There is a method buildExcelDocument in this class, which we should override to build the […]

  113. Joe says:

    Here in this context, I have used the word interface to express a Java method signature. Surely this is not Java interface class.

  114. Joe says:

    I am expressing that those words cannot be used interchangeably. When we say polymorphism, it is a whole larger concept comprising of all these things.

  115. Joe says:

    Yes it can be overloaded and no issues. But, the method with single String-array argument will be invoked as always.

  116. Winsotn says:

    in the puzzle given in your tutorial, obj.overload(null) is calling double array method. wanted to know why the object o method is not called? because even object o can be null.

  117. pratok says:

    We dnt need 2 define variable

  118. Baskar says:

    Hi Joe,

    Thanks for the information because There will be always confusion about the overriding and overloading. It is good that you have explained well.

    Thanks,

  119. venkat says:

    u r god to java developers

  120. sekhar says:

    simply create an object to child class and call the super class method

  121. sekhar says:

    hi joe,
    it would be better if you use a good example than this.

  122. amritansh says:

    Brother super explanation hats off bro.

  123. Farooq says:

    Hi Joe,

    Could you please explain why “double array argument method” is called in the puzzle? Thanks in advance.

  124. Anonymous says:

    I have a doubt. Every teacher call method overloading is a static polymorphism. But every method is binded with the object at the runtme. Because object created at run time. Then how it is static?

  125. Vikrant Singh says:

    Thanks a lot. Nice, clear, confusion-free explanation. While trying that code snippet of overloading with null object. it is showing errors in method definition. Help plz, anyone interested in discussing on that

  126. nivetha says:

    superb!thank you for giving the clear example .

Comments are closed for this "Overloading vs Overriding in Java" tutorial.

↑ Go to top