Introduction
In this article, we will discuss 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 overridden 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
- The argument parameter should be exactly the same as that of the overridden method.
- A method declared static cannot be overridden but can be re-declared.
- The return type should be the same or a subtype of the return type declared in the original overridden method in the super class.
- A method declared final cannot be overridden.
- Constructors cannot be overridden.
- If a method cannot be inherited then it cannot be overridden.
- A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.
- A subclass in a different package can only override the non-final methods declared public or protected.
- 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 is 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

Important key concept to remember
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 extend class Ab and inherits
its properties and we have to show how the class method get overloaded not
overridden we define a main class Override as shown below



Shobana JPosted Jul 7, 2016, 7:30 AM
Nice explanation
Mohammed IbrahimPosted Apr 8, 2016, 2:19 PM
nice
Rahul Kumar SaxenaPosted Apr 8, 2016, 7:40 AM
worth reading
Karthikeyan KPosted Apr 8, 2016, 3:45 AM
Thanks for sharing