Hi;
For Polymorphism; the semi code I can do is the following example;
/*
the code might be incomplete to run; since is written in gmail editor;
*/
abstract class Figure {
abstract public Boolean isWithinShape(float x, float y);
// other methods
}
class Circle extends Figure {
public Boolean isWithinShape(float x, float y) {
// class implementation
}
// other methods
}
class Square extends Figure {
public Boolean isWithinShape(float x, float y) {
// class implementation
}
// other methods
}
since Circle & Square are different shapes and are not equal to each other;
so the method above to check if the point (x,y) is within that shape should
be differetly implemented. Now assume an array of Figures like fig;
class Test {
public static void main(String[] args) {
Figure[] fig = new Figure[50];
for(int i = 0; i < 50; i = 2 * i) {
fig[i] = new Circle();
}
for(int i = 0; i < 50; i = 2 * i + 1) {
fig[i] = new Square();
}
// now checking if (2.5, 7) is within any figures;
i = 0;
while (!(fig[i].isWithinShape(2.5,7)) && (i < 50)) {
i++;
}
if ( i >= 50)
System.out.println("No Shape contains (2.5, 7).");
else
System.out.println("Shape " + i " contains (2.5, 7).");
}
}
The above example is easy to do with polymorphism; but if there was no
polymorphism; then we should check whether fig[i] is circle or square!!!!!!
then casting to the appropriate type and then calling its own method. but
the polymorphism makes it easy!
Have fun
&
With the hope of rising of Mahdi;
Ali Shakiba
Shahid Bahonar University of Kerman
Iran - Kerman
On Tue, Dec 22, 2009 at 8:58 PM, Tikeswar Mohanty <
[email protected]> wrote:
> i want a real example of polymersim and also what is the advantage ?
>
> --
> To post to this group, send email to
> [email protected]
> To unsubscribe from this group, send email to
> [email protected]<javaprogrammingwithpassion%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en