Java Object Oriented Programming

Method Overriding

If a subclass declares the method with same name and parameters along with same return type as present in super class, then this scenario is known as method overriding.

In order to override the method of any particular class to your class you need to inherit that class.


Criteria for method overriding

  • Class overriding the method of super class must inherit the super class. That means IS-A relationship is mandatory.
  • Overridden method must have same name as of parent class method.
  • Number of parameters in overridden method should be same as of parent class method.




Source: AdmissionForm.java

package polymorphism;

class AdmissionForm{
      public void Courses(int opt){

            System.out.println("computer science");
            System.out.println("electrical engineering");



Source: Student.java

class Student extends AdmissionForm{


      public void Courses(int choice){


                  case 1: System.out.println("computer science");


                  case 2: System.out.println("electronics");


                  case 3: System.out.println("electrical engineering");


                  default: System.out.println("Please make a decision between 1-3");




Source: StudentRam.java

public class StudentRam {

      public static void main(String[] args) {

            Student ram = new Student();




Why overriding is needed?

In case if there is a method in super class to which the sub class wants to provide its own specific implementation. In such scenarios method overriding is needed.

In the above code snippet, class AdmissionForm has method Courses() which is overridden by class Student method. And it has provided its own implementation to this method.


Other Important points:

  • Static method cannot be overridden. It’s because static method belongs to the class in which it is declared.
  • Since main() in java is static so it cannot be overridden as well.
  • Method overriding helps in achieving run time polymorphism.

Method overloading vs Method overriding

How to call super class method in case of method overriding?



Please Share this page
Views : 198
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!