Back to Top

Monday, 16 September 2013

JAVA: Overloading and Overriding


  Overloading is a popular technique of object oriented programming. It allows you to define a method with same name but with different parameters list.

 Java identifies method by its name and signature. So, if the combination of method name and signature is unique, the compiler doesn’t have any issue to identify it. When you use overloaded methods, the java compile will automatically identifies the right version of the method by number and/or types of passing parameters. But you can’t declare more than one method with same name and passing parameters. The compiler doesn’t consider return types when validating uniqueness of a method. That means you can’t have two methods with same name and passing parameters with different return type.

   Java allows both method and constructor overloading. Let’s see this with an example.



public class OverloadingDemo {
    * This class demonstrates overloading in java
    * @author
      private int a;
      /* constructor overloading */
      public OverloadingDemo(){
      public OverloadingDemo(int a){
            this.a = a;
      /* method overloading */
      public int add(int num){
            a +=num;
            return a;
      public int add(String str){
            a+= Integer.valueOf(str);
            return a;
      public int add(int num1, int num2){
                  a += num1;
                  a += num2;
                  return a;

      /* test overloading */
      public static void main(String[] args) {
            OverloadingDemo objOverloadingDemo1 = new OverloadingDemo();
            OverloadingDemo objOverloadingDemo2 = new OverloadingDemo(5);
            System.out.println(objOverloadingDemo1.add(3, 5));
            System.out.println(objOverloadingDemo2.add(3, 5));




   Here, in this class OverloadingDemo we defined two constructors with different signatures, and three methods with same name but different number and/or types of passing parameters.


     Overriding is the technique of inheriting a class and redefining the behaviors of the super class. That means, when you define a method in subclass with the same signature (name, plus the number and the type of its parameters) and return type as in the superclass, then you are overriding the method of the superclass in your subclass, and the technique is known as method overriding.

           In method overriding, method name, method input parameters and return types in subclass should be same in superclass.  It can only modify the behaviors. It can return a subtype of the original return type defined in superclass, and this technique is known as covariant return type [Java 1.5 or above support this]. For example, suppose the return type in superclass is java.util.Map, you can change it to java.util.HashMap in subclass, as HashMap is a subtype of Map.

           You can also use @Override annotation [which is available in Java 1.5 or above] to tell the compiler that you are trying to override a method in the superclass. And, fof any reason, if the compile is unable to detect that method, it will generate an error.

            In method overriding in java, you can level up the access level but you can’t level it down. That means, if a method is declared as protected in superclass, it can be set as public is subclass but not as private.

             If a method is declared as final, it can’t be overridden.

Now let’s see with an example.


Super Class :


import java.util.HashMap;
import java.util.Map;

public class SuperClass {
    * This is a sample class
    * @author
      /* the return type of this method is Map and in subclass it will be changed to HashMap */
      public Map methodForcovariant(){
            Map map = new HashMap();
            return map;
      /* sample method which willbe overridden in subclass */
    public void sampleMethod(){
    /* the specifier of this method willbe changed to public from protected in its subclass */
    protected void protectedMethod(){
    /* this method can't be overridden as it's declare as final */
    public final void finalMethod(){


Sub Class:


import java.util.HashMap;
import java.util.Map;

            public class OverridingDemo extends SuperClass{

                * This class demonstrates overriding by extending SuperClass
                * @author
                  /* Overriding with covariant return type :
                   * the return type of this method is modified to its subtype which is allowed in method overriding */
                public HashMap methodForcovariant(){
                        HashMap hmp = new HashMap();
                        return hmp;
                  /* Overriding:
                   * overriding the method SuperClass.sampleMethod() */
                  public void sampleMethod(){
                  /* Overriding access specifier:
                   * the specifier of this method is changed to public from protected */
                public void protectedMethod(){

            public static void main(String[] args) {
                  SuperClass objSuperClass = new SuperClass();
                  SuperClass objOverridingDemo = new OverridingDemo();





Here, we have used 4 methods:
methodForcovariant() – it shows the change of return type to its subtype during method overriding.
sampleMethod() – it shows method overriding in normal way.
protectedMethod() – it shows method overriding by leveling up access specifiers.
finalMethod() – it shows we can’t override methods declared as final.

12 Responses to “ JAVA: Overloading and Overriding ”

Anonymous said...
16 September 2013 at 13:07

nice post

Anonymous said...
31 December 2013 at 13:14


hitesh kumar said...
12 January 2016 at 07:33

Method Overriding in Java

Thanks for this post, keep sharing

navya said...
24 October 2016 at 14:59

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java 8 Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai Java Training Institutes

Java Training Institutes in Chennai Java Training in Chennai

shasthika said...
2 May 2017 at 16:45

perfect explanation about java programming .its very useful.thanks for your valuable training in chennai | java training in velachery

SEO Services said...
22 November 2017 at 13:16

It’s a very informative and helpful article, thank you for sharing!

CIITNOIDA - Best Oracle and linux training institute in noida delhi ncr said...
20 June 2018 at 13:39

Best B Tech college in Delhi
Best B Tech college in Noida
Best B Tech college in Gurgaon
Best B Tech college in UP
Best B Tech college in India
Best B Tech college in Hyderabad
Best B Tech college in Chennai
Best B Tech college in Bangalore
Best B Tech college in Pune

CIITNOIDA - Best Oracle and linux training institute in noida delhi ncr said...
28 June 2018 at 16:34

Best MCA

colleges in noida


colleges in noida

Best M

TECH colleges in noida

Praveen H said...
29 October 2018 at 00:00

nice article, very informative.
for java interview programs visit: Java programming

Unknown said...
8 November 2018 at 15:08

Very useful information that you have shared and it is very useful to me. Thanks for sharing the information with us.
kindle mobi conversions

abc said...
14 August 2019 at 01:26

I learn a lots of things here. It is an amazing post , thanks for help me.
students can learn programming on python programming tutorial
java programming language
cpp programming language

Hitesh Kumar said...
10 May 2020 at 22:56

method overloading in java
Really nice your article thanks for sharing this article

Post a comment

Popular Posts

All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar
blog links Web Directory Academics Blogs