Method Overriding in Java - Java @ Desk

Saturday, March 1, 2014

Method Overriding in Java

Method Overriding in Java
Method overriding in Java is a feature that allows the methods of sub classes to provide their defined implementations. In case of overriding, the method may use @Override annotation.

This feature comes into picture when we talk of inheritance. When a sub class extends the super class, sub class inherits all the public properties and public methods of the super class. When a sub class needs to provide their own implementation for the overridden method or fields that is being done using the method overridding.

Overriding in Java simply means that the particular method would be called based on the run time type of the object and not on the compile time type of it.

Can static methods override?

The answer is No. Static methods belongs to the class and not to object. If the parent class is having static method, then that method belongs to parent class only. We make a call to static method using the Class and not the Object.

Even though the sub class defines the static method following all overridden rules, that method will still belong to sub class and its being called using the Sub Class.

Even if you use the object of sub class (where to object is of Super Class type) to call the static method, it will still call the static method of superclass only.

 
Can final methods override?

The answer is No. Final methods in super class cannot be overridden in a sub class. If you do so, a compilation error occurs which states that "Cannot override the final method from SuperClass"

 
Can private methods override?

The answer is No. Private members and methods of the class belongs to that class only and are non visible outside the class.

 
Rules for Overridden Methods

1) Method in sub class should have same name as of super class
2) Method's return type in sub class should have same return type of method in super class
3) Number of arguments in a method of sub class should be equal to the number of arguments in a method of super class
4) Order of arguments in a method of sub class should match with the order of arguments in a method of super class
5) The access level cannot be more restrictive than the overridden method's access level
6) Final & Private methods cannot be overridden
7) 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
8) Constructors cannot be overridden

This is the most common and most confusing interview question that is being asked by most interview panels.

Sample Implementation

SuperClass.java
package com.overriding;

public class SuperClass {

 public void getMbaDegree() {
  System.out.println(SuperClass.class.getSimpleName()
    + " - getMbaDegree()");
 }

 public void getInterestOnCreditCard(String string, Integer integer) {
  System.out.println(SuperClass.class.getSimpleName()
    + " - getInterestOnCreditCard()");
 }

 public static void getDomainName() {
  System.out.println(SuperClass.class.getSimpleName()
    + " - getDomainName()");
 }
 
}


SubClass.java
package com.overriding;

public class SubClass extends SuperClass {
 public void getMbaDegree() {
  System.out.println(SubClass.class.getSimpleName()
    + " - getMbaDegree()");
 }

 public void getInterestOnCreditCard(String string, Integer integer) {
  System.out.println(SubClass.class.getSimpleName()
    + " - getInterestOnCreditCard()");
 }

 public static void getDomainName() {
  System.out
    .println(SubClass.class.getSimpleName() + " - getDomainName()");
 }

}


TestOverridden.java
package com.overriding;

public class TestOverridden {

 public static void main(String args[]) {
  SubClass subClass = new SubClass();
  subClass.getMbaDegree();
  subClass.getInterestOnCreditCard("", 10);
  SubClass.getDomainName();
  
  System.out.println("\n");
  
  SuperClass superClassReference = new SubClass();
  superClassReference.getMbaDegree();
  superClassReference.getInterestOnCreditCard("", 10);
  SuperClass.getDomainName();
 }
}



SubClass - getMbaDegree()
SubClass - getInterestOnCreditCard()
SubClass - getDomainName()


SubClass - getMbaDegree()
SubClass - getInterestOnCreditCard()
SuperClass - getDomainName()
SuperClass - getDomainName()

To download source, click here






No comments:

Post a Comment