0
4.1kviews
Explain method overloading and method overriding with suitable example.

Similar questions

1) Short note on method overloading

2) Compare and Contrast overriding methods and overloading methods with suitable examples

Marks: 5 M, 8 M, 10 M

Year: Dec 2013, May 2014, Dec 2013, May 15

1 Answer
0
78views
  • In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.
  • When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass.
  • The version of the method defined by the superclass will be hidden. Consider the following:

    // Method overriding.
    class A 
    {
    int i, j;
    A(int a, int b) 
    {
    i = a;
    j = b;
    }
    // display i and j
    void show() {
    System.out.println("i and j: " + i + " " + j);
      }
      }
    class B extends A 
    {
    int k;
    B(int a, int b, int c) 
    {
    super(a, b);
    k = c;
    }
    // display k – this overrides show() in A
    void show() 
    {
    System.out.println("k: " + k);
    }
    }
    class Override 
    {
    public static void main(String args[]) {
    B subOb = new B(1, 2, 3);
    subOb.show(); // this calls show() in B
    }
    }
    
  • The output produced by this program is shown here: k: 3

  • When show( ) is invoked on an object of type B, the version of show( ) defined within B is used. That is, the version of show( ) inside B overrides the version declared in A. Method overriding occurs only when the names and the type signatures of the two methods are identical.
  • Though, names return types and parameters of both the methods are same their definition is different.
  • If they are not identical, then the two methods are simply overloaded. For example, consider this modified version of the preceding example:

    // Methods with differing type signatures are overloaded – not overridden.
    class A
    {
    int i, j;
    A(int a, int b) 
    {
    i = a;
    j = b;
    }
    // display i and j
    void show() 
    {
    System.out.println("i and j: " + i + " " + j);
    }
    }
    // Create a subclass by extending class A.
    class B extends A 
    {
    int k;
    B(int a, int b, int c) 
    {
    super(a, b);
    k = c;
    }
    // overload show()
    void show(String msg)
    {
    System.out.println(msg + k);
    }
    }
    class Override 
    {
    public static void main(String args[]) 
    {
    B subOb = new B(1, 2, 3);
    subOb.show("This is k: "); // this calls show() in B
    subOb.show(); // this calls show() in A
    }
    }
    
  • The output produced by this program is shown here:

    This is k: 3

    i and j: 1 2

  • The version of show( ) in B takes a string parameter. This makes its type signature different from the one in A, which takes no parameters. Therefore, no overriding (or name hiding) takes place.

Overloading methods Overriding methods
Happening within the same class. Happening between super class and sub class.
Method signature should not be same. Method signature should be same.
It happen at time of compliance or we can say overloading is the early binding or static binding. It happen on time of run time or we can say overriding is dynamic binding or let binding.
Method can have any return type Method return type must be same as super class method
Method can have any access level. Method must have same or wide access level than super class method access level.
Please log in to add an answer.