Bright Java Tutorial

is brought to you by DoITBright LLC

Java

Overriding

Overriding is an Object Oriented Programming Concept that allows the child class or subclass to provide its own implementation of the method that is already declared in its parent class or classes that are in the higher inheritance hierarchy. Let us study the example below to demonstrate the concept of overriding and it's rules.

  package com.example.core.oop2;

  /**
   * This is to demonstrate the concept of Overriding in 
   * Object Oriented Programming using Java.
   * 
   * @author Rolan Liwanag
   *
   */
  public class Parent {

    public void method1(String param) {
      System.out.println("Parent method1");
    }
	
    public void method2() {
      System.out.println("Parent method2");
    }
	
    public final void method3() {
      System.out.println("Parent method3");
    }
	
    public static void method4() {
      System.out.println("Parent method4");
    }
	
    private void method5() {
      System.out.println("Parent method5");
    }
	
    public void method6() throws NumberFormatException {
      System.out.println("Parent method6");
    }
  }
                            

  package com.example.core.oop2;

  import java.io.IOException;

  public class Child extends Parent {

    /*
     * Rule for Overriding : The overriding method should 
     * have exactly the same argument list as that of the 
     * method being overridden. In this case, String as an 
     * argument that the method accepts.
     * 
     * Rule for Overriding : The return type should be the 
     * same or at least a sub type of the returned 
     * parameter of the overridden method. In this
     * case, it is void. No return parameter.
     */
    public void method1(String param) {
      System.out.println("Child method1");
    }
	
    /*
     * Rule for Overriding : The access modifier for the 
     * overriding method cannot be more restrictive that 
     * the access modifier of the method being overridden.
     * In this case, Parent method2 has access modifier of 
     * 'public'. We cannot declare the Child method2 with 
     * an access modifier other than 'public' since
     * public is the most non-restrictive access modifier. 
     * Try changing the 'public'
     * to 'protected' or 'private'. It wont compile.
     */
    //protected void method2() {
    public void method2() {
      System.out.println("Child method2");
    }
	
    /*
     * Rule for Overriding : A method declared as final 
     * cannot be overridden. In this case, Parent method3 
     * is declared as final. Hence, we cannot override 
     * that method. Try uncommenting the commented method3 
     * below. It will have compilation error.
     */
    /*public void method3() {
      System.out.println("Child method3");
    }*/
	
    /*
     * Rule for Overriding : Methods declared static 
     * cannot be overridden but can be re-declared. 
     * In this case, both Parent and Child have a 
     * static method named method4. However, this is not 
     * considered overriding since both methods are static.
     */
    public static void method4() {
      System.out.println("Child method4");
    }
	
    /*
     * Rule for Overriding : If a method cannot be 
     * inherited, it cannot be overridden. In this case, 
     * though both Parent and Child declared a method with 
     * the same name, argument list, return type and 
     * access modifier, the Parent method5 cannot be 
     * overridden because it has a 'private' access 
     * modifier. The 'private' means it is only accessible 
     * within itself. Just like when you say, my shirt is 
     * a private property. Other people just don't use 
     * your shirt.
     */
    private void method5() {
      System.out.println("Child method5");
    }
	
    /*
     * Rule for Overriding : The overriding method cannot 
     * throw a broader checked exception or an exception 
     * that is not originally in the list of exceptions 
     * the overridden method is throwing. Though, the 
     * overridng method can throw any unchecked exceptions.
     */
    /*public void method6() throws NumberFormatException, 
      IOException {*/ 
    //IOException is not in the overridden method
    //public void method6() throws Exception { 
    //Exception is broader than NumberFormatException
    public void method6() throws IndexOutOfBoundsException{ 
      // can be thrown since this is an unchecked exception
      System.out.println("Child method6");
    }
	
    public static void main(String[] args) {
      Child cObj = new Child();
      cObj.method1("Test");
      /*
       * As you may have noticed, both Parent and Child 
       * class contains methods named method1 with the 
       * same parameter list and type and both returns no 
       * parameter. Rule is satisfied. So, when cObj which 
       * is an instance of the Child class made a call to 
       * method1, it is its own method1 that was invoked.
       */
      cObj.method2();
      /*
       * Parent and Child methods named method2 are both 
       * dedclared as public. Rule in access modifier is 
       * satisfied.
       */
      cObj.method3();
      /*
       * A final method cannot be overridden. 
       * Thus, Parent method3 was invoked.
       */
      Child.method4();
      /*
       * Methods declared static cannot be overridden but 
       * can be re-declared. Since method4 is static, it 
       * means class level NOT object level. So, it means, 
       * method4 should be accessed in a static way. 
       * Though, if we access it like cObj.method4() wont 
       * have any compilation problems, it is not 
       * proper to do so.
       */
      cObj.method5();
      /*
       * Private methods cannot be overridden.
       */
      cObj.method6();
    }
  }
                            



Back    Next