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 by different ways. Polymorphism is derived from two Greek words, which are:

  • poly - many
  • morphs- forms

Simple program 

  1. class Box  
  2. {  
  3. int w,h;  
  4.   
  5. void info()  
  6.   {  
  7.     System.out.println("This is a simple box");  
  8.     System.out.println("width = "+ w + " hieght "+ h);  
  9.   }  
  10. }  
  11.   
  12. class WoddenBox extends Box  
  13. {  
  14.   void info()  
  15.      {  
  16.         System.out.println("This is a Wodden box");  
  17.      }  
  18. }  
  19.   
  20. class SteelBox extends Box  
  21. {  
  22.   void info()  
  23.      {  
  24.         System.out.println("This is a steel box");  
  25.      }  
  26.  }  
  27.   
  28. class LargeWoddenBox extends WoddenBox  
  29. {  
  30.   void info()  
  31.      {  
  32.         System.out.println("This is a Huge Wodden box");  
  33.      }  
  34.  }  
  35.   
  36. class test5  
  37. {  
  38. public static void main ( String arg[] )  
  39.         {  
  40.               Box b1 =new Box();  
  41.               WoddenBoxwb = new WoddenBox();  
  42.               SteelBox s1 = new SteelBox();  
  43.               LargeWoddenBox p1 = new LargeWoddenBox();            
  44.   
  45.               b1.info();  
  46.               wb.info();  
  47.               s1.info();  
  48.               p1.info();  
  49.         }  

Explanation

In this blog, I explained about Polymorphism concept in Java programming. The output will be displayed in the Run module.

Output
 
Ebook Download
View all
Learn
View all