Taner Şenyurt

Kişisel gelişim sitesi…

Polymorphism (Overloading)

May19

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. using namespace std;
  6.  
  7. class room{
  8. public:
  9. virtual void clean(){ cout<<"tried to clean but not success\n"<<endl; } // Polymorph method (virtual)
  10. void burn(){ cout<<"room burned \n"<<endl; }
  11. };
  12.  
  13. class living : public room{ //inherit to room it means it has a same properties of room  
  14. void clean();
  15. };
  16.  
  17. void living::clean(){
  18. cout<<"living room cleaned ! \n"<<endl; //clean method is used polymorph in here
  19. }
  20.  
  21. class bed : public room{
  22. //something about bedroom
  23. };
  24.  
  25. main()
  26. {
  27. room *xroom;
  28.  
  29. xroom = new bed; //created an object from bed
  30. xroom->clean(); //it should say tried to clean but not success
  31.  
  32. delete xroom; // free your mind :)
  33.  
  34. xroom = new living; // living object
  35. xroom->clean(); //it should say living room cleaned!
  36.  
  37. return 0;
  38. }

it’s output in the picture.

One Comment to

“Polymorphism (Overloading)”

  1. On May 25th, 2009 at 4:48 am holaLoyatly Says:

    Hi, nice posts there :-) thank’s concerning the gripping information