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.
-
-
#include <stdio.h>
-
#include <string.h>
-
#include <iostream>
-
using namespace std;
-
-
class room{
-
public:
-
virtual void clean(){ cout<<"tried to clean but not success\n"<<endl; } // Polymorph method (virtual)
-
void burn(){ cout<<"room burned \n"<<endl; }
-
};
-
-
class living : public room{ //inherit to room it means it has a same properties of room
-
void clean();
-
};
-
-
void living::clean(){
-
cout<<"living room cleaned ! \n"<<endl; //clean method is used polymorph in here
-
}
-
-
class bed : public room{
-
//something about bedroom
-
};
-
-
main()
-
{
-
room *xroom;
-
-
xroom = new bed; //created an object from bed
-
xroom->clean(); //it should say tried to clean but not success
-
-
delete xroom; // free your mind
-
-
xroom = new living; // living object
-
xroom->clean(); //it should say living room cleaned!
-
-
return 0;
-
}
it’s output in the picture.
Hi, nice posts there
thank’s concerning the gripping information