CodesJava

Easy learning with example program codes

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 CodesJava
 */
class Student {
      /**
       * This method is used to show details of a student.
       * @author CodesJava
       */
      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 CodesJava
       */
      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.

Download this example.

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 CodesJava
 */
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

Download this example.
 
Next Topic: Association in java with example.
Previous Topic: Method overriding 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.

 

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap