|
|
| core java - Use of polymorphism in object oriented programming |
Expert:harikrishna
<p>Hi all, I am digging for an example of polymorphism in object orient programming. Can anyone please help?</p> |
| Answers |
Use of polymorphism in object orient programming – using Overloading and Overriding Concept<br /><br />Polymorphism<br />It is a basic feature in OOP’s Strategy of programming emulated or being followed by high-level languages .<br />In java it states using of one single interface with a set of actions that is like one name may be referred to different functionality .<br /><br />For example in creating a java program we create method<br /> void Amount( )<br />{<br /> -----<br /> -----<br />}<br /><br />Amount (int xyz)<br />{<br /> ---<br /> ---<br />}<br />Amount(int cat)<br />{<br />}<br /><br />Now see carefully here in the small brackets of same method I have assigned three different”int amt, int xyz, int cat” parameters<br />(parameters are value data like that we assign in method)<br />this is called Overloading <br /><br />And in case of same parameters it is said Overriding.<br /><br />See In both cases method “Amount()” is being more than one time and this what above definition say “using of one single interface with a set of actions”.<br /><br />For further details with examples concerning Encapsulation, Inheritance and Polymorphism chech undersigned URL.<br /><br /><a href="http://www.roseindia.net/java/learn-java-in-a-day/oops.shtml" target="_blank">http://www.roseindia.net/java/learn-java-in-a-day/oops.shtml</a><br /><br /><br />BaadshahKhan
|
What is polymorphism?what is the exactly use of it?how we can use polymorphism in object orient programming.<br /><br />Polymorphism :-<br /><br />Polymorphism means the ability to take more than one form. Polymorphism means 'any forms.' In OOP, it refers to the capability of objects to react differently for the same method.<br /><br />In OOP methodoverloading refers to the capability of objects to react differently for the same method. Polymorphism can be implemented in Java language in form of multiple methods having the same method name. Java code uses a late-binding for supporting polymorphism, the method which is invoked is decided at runtime.<br /><br />In polymorphism by using a same function name with the different signature we can perform the multiple task this is called method overloading & using a same function name with the same signature we can extend the functionality of the superclass functions according to our requirement.<br /><br />In java Polymorphism can be used in two ways :<br /><br />1. Method Overloading<br /><br />2. Method Overriding <br /><br />1. Method Overloading :-<br />Overloaded methods are methods have the same name, but different argument lists. <br />Overloaded methods have the same names but different argument lists. The arguments may differ in type or number, or both. However, the return types of overloaded methods can be the same or different. <br /><br />An example of the method overloading is given below:<br /><br />class methodOverloading{<br />int add( int a,int b)<br />{<br />return(a+b);<br />}<br /><br />float add(float a,float b)<br />{<br />return(a+b);<br />}<br />double add( int a, double b,double c)<br />{<br />return(a+b+c);<br />}<br />}<br />class mainClass extends methodOverloading <br />{<br />public static void main( String arr[] )<br />{<br />mainClass temp = new mainClass();<br />System.out.println(temp.add(10,20));<br />System.out.println(temp.add(1.5f,2.3f));<br />System.out.println(temp.add(10,20.4,25.6));<br />}<br />}<br /><br /> The output of the above program is given below:<br /><br />30<br /><br />3.8<br /><br />56.0<br /><br />2. Method Overriding :-<br />Overriding means when a subclass method has the same name, same return type, and same argument list as the superclass method.<br /><br />class methodOverriding<br /><br />{<br />String showMessage()<br />{<br />return("THIS IS SUPERCLASS METHOD");<br /><br />}<br />}<br />class mainClass extends methodOverriding <br />{<br />public static void main( String arr[] )<br />{<br />String showMessage()<br />{<br />System.out.println("THIS IS SUBCLASS METHOD");<br />}<br />mainClass temp = new mainClass();<br />System.out.println(temp.showMessage());<br />}<br /><br />}<br /><br />The output of the above program is given below:<br /><br />THIS IS SUBCLASS METHOD
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|