Sponsored Links
Ad by Google
One of the important concept of core java inheritance module. Dynamic method dispatch is also known as late binding or runtime polymorphism. In this tutorial, I am going to explain what is dynamic method dispatch and how to use them in practically with simple example. This topic is very popular in java,j2ee interview like why String is immutable in java
What is Dynamic method dispatch?
Dynamic method dispatch, it's very clear from the statement itself dynamic(runtime) method dispatch(which method to call), it determines which methods needs to be called at runtime and it's a part of inheritance relationship, that's it nothing more than that just determines which method needs to be called at runtime.
Example,
Output
137g, 5MP, 3.5 Inc
133g, 2MP, 3.5 Inc
What is Dynamic method dispatch?
Dynamic method dispatch, it's very clear from the statement itself dynamic(runtime) method dispatch(which method to call), it determines which methods needs to be called at runtime and it's a part of inheritance relationship, that's it nothing more than that just determines which method needs to be called at runtime.
Example,
package com.javamakeuse.dmd; public class IPhone { public String getSpecifications() { return "135g, 2MP, 3.5 Inc"; } public static void main(String[] args) { IPhone iPhone3 = new IPhone3(); IPhone iPhone4 = new IPhone4(); printIPhoneSpecs(iPhone4); printIPhoneSpecs(iPhone3); } public static void printIPhoneSpecs(IPhone iPhone) { System.out.println(iPhone.getSpecifications()); } } class IPhone3 extends IPhone { @Override public String getSpecifications() { return "133g, 2MP, 3.5 Inc"; } } class IPhone4 extends IPhone { @Override public String getSpecifications() { return "137g, 5MP, 3.5 Inc"; } }The printIPhoneSpecs method takes IPhone instance as an argument, and it will determines at runtime which getSpecification methods needs to be called.
Output
137g, 5MP, 3.5 Inc
133g, 2MP, 3.5 Inc
Sponsored Links
0 comments:
Post a Comment