Method Overriding In Java

Introduction

In this article we will discuss about method overriding in JAVA.

What is Method Overriding

In a class hierarchy, when a method in a sub class 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 overriden method is called from within a sub-class, it will always refer to the versions of that method defined by the subclass.

Some Rules for method overriding

  1. The argument parameter should be exactly the same as that of the overridden method.
  2. A method declared static cannot be overridden but can be re-declared.
  3. The return type should be the same or a subtype of the return type declared in the original overridden method in the super class.
  4. A method declared final cannot be overridden.
  5. Constructors cannot be overridden.
  6. If a method cannot be inherited then it cannot be overridden.
  7. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.
  8. A subclass in a different package can only override the non-final methods declared public or protected.
  9. An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method.

Method versions are hidden. Let's take an example.

Defining a class Ab which are to be override

// method overriding
class
Ab
  {
    int i,j;
    Ab(int a, int b)
      {
        i=a;
        j=b;
      }
    //
display i and j
    void
show()
      {
        System.out.println("i and j: " +i+" " + j );
      }
  }

Defining another class B which inherit the previous one (class Ab) and define a class Override containing main method which shows the value of K as shown in fig.

class B extends Abc
  {
    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
      }
  }

Output

overriding1.jpg

Important key concept to remeber

Method overriding occurs only when the names and the arguments of the two methods are identical. If they are not similar, then the methods are simply overloaded. For example

// Method with different type signatures are overloaded-not
//overridden
class
Ab
  {
    int i,j;
    Ab(int a, int b)
      {
        i=a;
        j=b;
      }
    //
display i and j
    void
show()
      {
        System.out.println("i and j: " +i+" " + j );
      }
  }

Create other class B- in this class we extends class Ab and inherits its properties and we have to shown how the class method get overloaded not overriden we define a main class Override as shown below

class B extends Ab
  {
    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
      }
  }

Output

overriding3.jpg

Why Overridden Methods

As started earlier, overridden methods allow Java to support run-time polymorphism. Polymorphism is essential to object-oriented programming for one reason: it allows a general class to specifry methods that will be common to all its derivatives, while allowing subclass to define the specific implementation of some or all of those methods. Overridden methods are another way that Java implements the "one interface, multiple methods" aspect of polymorphism.

Dynamic, run-time polymorphism is one of the most powerful mechanism that object-oriented design brings to bear on code reuse and robustness. The ability of exciting code libraries to call methods on instances of new classes without recompiling while maintaining a clean abstract interface is a profoundly powerful tool.

Applying Method Overriding

The below program creates a superclass class Figure, also define another method area() which is use to find the area.

Create a class Fig which define body structure of fig and passing two variable val1 and val2, to find the area.

// using run time polymorphism.
class Fig
  {
    double val1;
    double val2;
    Fig(double a, double b)
      {
        val1=a;
        val2=b;
      }
    double area()
      {
        System.out.println("Area for figure is undefined.");
        return 0;
      }
  }

Create another class Rect which contain Rectangle structure

class Rect extends Fig
  {
    Rect(double a, double b)
      {
        super(a, b);
      }
    //
override area for Rectangle
    double area()
      {
        System.out.println("Inside Area for Rectangle:");
        return val1*val2;
      }
  }

Create another class Tri which contain triangle structure

class Tri extends Fig
  {
    Tri(double a, double b)
      {
        super(a, b);
      }
    //
override area for right triangle
    double area()
      {
        System.out.println("Inside area for Triangle.");
        return val1*val2/2;
      }
  }

Create a main class FindAreas which is used to print the area of Rectangle, Triangle and Figure.

class Findareas
  {
    public static void main(String args[])
      {
        Fig f=new Fig(10, 10);
        Rect r=new Rect(9, 5);
        Tri t=new Tri(10, 8);
        Fig figref;
        figref=r;
        System.out.println("Area is " + figref.area());
        figref=t;
        System.out.println("Area is " + figref.area());
        figref=f;
        System.out.println("Area is " + figref.area());
      }
  }

Output

overridng2.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all