-->

14. Dynamic method dispatch in java

Method Overriding in Java: Previous                                                    Next: Association In Java                    

Dynamic method dispatch in java

Dynamic method dispatch is a mechanism to resolve overridden method call at run time instead of compile time. It is based on the concept of up-casting (A super class reference variable can refer subclass object.).

Example 1:

/**
 * This program is used for simple method overriding example.
 * with dynamic method dispatch.
 * @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[]){
       //Super class can contain subclass object.   
       Student obj = new CollegeStudent();
 
       //method call resolved at runtime
       obj.show();
      }
}

Output:

College Student details.
School Student details.

Example 2:

/**
 * 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[]){
           //Super class can contain subclass object.   
           Student obj = new CollegeStudent();
           //method call resolved at runtime
           obj.show();
      }
 
}

Output:

College Student details.

Note: Only super class methods can be overridden in subclass, data members of super class cannot be overridden.

/**
 * This program is used for simple method overriding example.
 * with dynamic method dispatch.
 * @author javawithease
 */
class Student {
      int maxRollNo = 200;
}
 
class SchoolStudent extends Student{
      int maxRollNo = 120;
}
 
class CollegeStudent extends SchoolStudent{
      int maxRollNo = 100;
}
 
public class StudentTest {
      public static void main(String args[]){
       //Super class can contain subclass object.   
       Student obj1 = new CollegeStudent();
       Student obj2 = new SchoolStudent();
 
       //In both calls maxRollNo of super class will be printed.
       System.out.println(obj1.maxRollNo);
       System.out.println(obj2.maxRollNo);
      }
}

Output:

200
200

Method Overriding in Java: Previous                                                    Next: Association In Java