• August 24, 2014
  • Core Java
  • Comments Off on Method overriding in java

Method overriding in java

If a subclass provides a method with the same signature (name and parameter) as in its super class, then subclass overrides the method of its super class. This process of overriding a super class method by subclass is known as method overriding.

Conditions for method overriding:

  1. 1. Method in subclass must have same signature as in its super class.
  2. 2. Two classes must follow IS-A relationship.

Example:

/**
 * This program is used for simple method overriding example.
 * @author javawithease
 */
class Student {
      /**
       * This method is used to show details of a student.
       * @author javawithease
       */
      public void show(){
            System.out.println("Student details.");
      }
}
 
public class CollegeStudent extends Student {
      /**
       * This method is used to show details of a college student.
       * @author javawithease
       */
      public void show(){
            System.out.println("College Student details.");
      }
 
      //main method
      public static void main(String args[]){
       CollegeStudent obj = new CollegeStudent();
       //subclass overrides super class method
       //hence method of CollegeStudent class will be called.
       obj.show();
      }
}

Output:

College Student details.

Download this example.

Can static method be overridden?

No, Static methods can’t be overridden because they are associated with class not with the object.

Role of access modifiers in method overriding:

Access modifier of overridden method in subclass can’t be more restrictive than in super class. Otherwise it will throw an exception.

 Example:

/**
 * This program is used for simple method overriding example.
 * @author javawithease
 */
class Student {
      /**
       * This method is used to show details of a student.
       * @author javawithease
       */
      public void show(){
            System.out.println("Student details.");
      }
}
 
public class CollegeStudant extends Student {
      /**
       * This method is used to show details of a college student.
       * @author javawithease
       */
      protected void show(){//compile-time error
            System.out.println("College Student details.");
      }
 
      //main method
      public static void main(String args[]){
       CollegeStudant obj = new CollegeStudant();
       //subclass overrides super class method
       //hence method of CollegeStudant class will be called.
       obj.show();
      }
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem:
Cannot reduce the visibility of the inherited method from Student
at com.javawithease.business.CollegeStudent.show(CollegeStudent.java:22)
at com.javawithease.business.CollegeStudent.main(CollegeStudent.java:30)

Download this example.
 
Next Topic: Dynamic method dispatch or Runtime polymorphism in java with example.
Previous Topic: Method overloading in java with example.

Related Topics:

Method overloading in java with example.
Method overriding in java with example.
Dynamic method dispatch or Runtime polymorphism in java with example.
Association in java with example.
Inheritance in java with examples.
Aggregation in java with example.

 


Comments are closed.