Font Awesome Icons

Tutorial

Runtime Polymorphism in Java

Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. When an overridden method is called through a superclass reference, Java determines which version(superclass/subclasses) of that method is to be executed based upon the type of the object being referred to at the time the call occurs. Thus, this determination is made at run time.

 

class Game

public void type() 

{    

System.out.println(“Indoor & outdoor”);   

}

}

class Cricket extends Game

public void type() 

{    

System.out.println(“CRICKET:outdoor game”);   

}  

}

class Hockey extends Game

public void type() 

{    

System.out.println(“HOCKEY :OUTDOOR game”);   

}  

}

class Main2

public static void main(String[] args) 

{   

Game gm = new Cricket();   

Game gm1 = new Hockey();   

gm.type();gm1.type(); 

}

}

Hi, Welcome to etechvidya