Java - Polymorphism

Introduction

 
In this blog, I will explain about the Polymorphism concept in Java. It is very simple in Java programming. The output will be displayed in the Run module.
  

Software Requirement

 
JDK1.3.
 

Polymorphism

 
Polymorphism in Java is a concept, by which, we can perform a single action in different ways. Polymorphism is derived from two Greek words, which are:
  • poly - many
  • morphs- forms
Simple program 
  1. class Box {  
  2.  int w, h;  
  3.  void info() {  
  4.   System.out.println("This is a simple box");  
  5.   System.out.println("width = " + w + " hieght " + h);  
  6.  }  
  7. }  
  8. class WoddenBox extends Box {  
  9.  void info() {  
  10.   System.out.println("This is a Wodden box");  
  11.  }  
  12. }  
  13. class SteelBox extends Box {  
  14.  void info() {  
  15.   System.out.println("This is a steel box");  
  16.  }  
  17. }  
  18. class LargeWoddenBox extends WoddenBox {  
  19.  void info() {  
  20.   System.out.println("This is a Huge Wodden box");  
  21.  }  
  22. }  
  23. class test5 {  
  24.  public static void main(String arg[]) {  
  25.   Box b1 = new Box();  
  26.   WoddenBoxwb = new WoddenBox();  
  27.   SteelBox s1 = new SteelBox();  
  28.   LargeWoddenBox p1 = new LargeWoddenBox();  
  29.   b1.info();  
  30.   wb.info();  
  31.   s1.info();  
  32.   p1.info();  
  33.  }  
  34. }   
Explanation
 
In this blog, I explained about Polymorphism concept in Java programming. The output will be displayed in the Run module.

Output
 
n