Polymorphism (Overloading)

Polymorphism { Overloading } means simply, same methods makes different jobs in different objects.Like how ?
Let’s say we are writing java code and we are using JCheckBox ,JTextField and JButton components.When we try to use setText method for all of them the response suppose to be same,but checkbox writes the text near to checkbox icon ,textfield writes to text into itself and button object writes to text on the button.It shows us the polymorphism because same method worked different for different object.This property called polymorphism or overloading.

There is a code below the page.It is written in c++ and it is the simple example for polymorphism and inheritance.

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <iostream>
  5. "tried to clean but not success\n"<<endl; } // Polymorph method (virtual)
  6. "room burned \n"<<endl; }
  7. };
  8.  
  9. class living : public room{ //inherit to room it means it has a same properties of room  
  10. "living room cleaned ! \n"<<endl; //clean method is used polymorph in here
  11. }
  12.  
  13. class bed : public room{
  14. //something about bedroom
  15. };
  16.  
  17. main()
  18. {
  19. room *xroom;
  20.  
  21. xroom = new bed; //created an object from bed
  22. xroom->clean(); //it should say tried to clean but not success
  23.  
  24. delete xroom; // free your mind 🙂
  25.  
  26. xroom = new living; // living object
  27. xroom->clean(); //it should say living room cleaned!

it’s output in the picture.

Tags: , , ,

Tuesday, May 19th, 2009 C & C++, Java, Programming, Uncategorized

1 Comment to Polymorphism (Overloading)

  • holaLoyatly says:

    Hi, nice posts there 🙂 thank’s concerning the gripping information