Core Java Tutorial

Core Java Tutorial

What is Method Overriding in Java

Method overriding:

Overriding is creating an instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass.

Example:
User.java
package com.candidjava.core;

public class User

{

 public void salary()

 {

  System.out.println("common salary functionality");

 }

}
Student.java
package com.candidjava.core;

public class Student extends User

{

 public void salary()

 {

  System.out.println("logic specific to student class and its subclass");

 }

}

Rules for overriding

  • Overriding method MUST have the same argument list (if not, it might be a case of overloading).
  • Overriding method MUST have the same return type; the exception is co-variant return (used as of Java 5) which returns a type that is a subclass of what is returned by the overridden method.
  • Overriding method MUST NOT have more restrictive access modifier, but MAY have less restrictive one.
  • Overriding method MUST NOT throw new or broader checked exceptions, but MAY throw fewer or narrower checked exceptions or any unchecked exceptions.
  • Abstract methods MUST be overridden in its first non-abstract subclass.
  • Final methods CANNOT be overridden.

Is it possible to override Constructor?

No, since constructor cannot be inherited it cannot be overridden.

Is it possible to Override Static Method?

No. If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.

Legal and Illegal overriding

Legal: Overriding with less restrictive access modifiers

package com.candidjava.core;

public class User

{

 protected void salary()

 {

  System.out.println("common salary functionality");

 }

}

 

package com.candidjava.core;

public class Student extends User

{

 public void salary() //legal to have less restrictive access modifiers

 {

  System.out.println("logic specific to student class and its subclass");

 }

}

Illegal: 

Overriding with more restrictive access modifiers.

package com.candidjava.core;

public class User

{

 protected void salary()

 {

  System.out.println("common salary functionality");

 }

}

 

package com.candidjava.core;

public class Student extends User

{

 private void salary() //Illegal to have more restrictive access modifiers

 {

  System.out.println("logic specific to student class and its subclass");

 }

}

Legal: Overriding with narrower checked exception

package com.candidjava.core;

public class User

{

 public void salary() throws Exception

 {

  System.out.println("common salary functionality");

 }

}

 

package com.candidjava.core;

public class Student extends User

{

 public void salary() throws Exception

 {

  System.out.println("logic specific to student class and its subclass");

 }

}

Illegal: Overriding final method

package com.candidjava.core;

public class User {

 final public void salary() throws Exception {
  System.out.println("common salary functionality");
 }
}

 

package com.candidjava.core;

public class Student extends User

{

 public void salary()

 {

  System.out.println("logic specific to student class and its subclass");

 }

}