hi my name is srikanth.can anyone briefly explain the concept of polymorphism to me in java context. thank you.

Polymorphism means many forms. The idea of polymorphism is very important in OOP. Lets look at Java's IO classes (they use a lot of this concept).

The code below is legal:

//The inputstream r will get its input from a file. 
InputStream r = new FileInputStream("someFile.txt");

The code below is also legal:

//The inputstream r will get its input from a byte array
byte[] someBytes = new byte[500];
InputStream r = new ByteArrayInputStream( someBytes );

Therefore, if someone gives you an "InputStream" object and asks you "where do it gets it's input?". You CANNOT answer this question b/c it could be from a file or a byte array or from the network.

When you call the method r.read() is it calling the read method in FileInputStream, or the read method in ByteArrayInputStream? You CANNOT answer this question either because r is an InputStream and you can't tell if its really a FileInputStream or a ByteArrayInputStream.

Therefore, the read() method has many forms. If you call it, it might be getting data from a file, or from the network, or from a byte array. You just don't really know. Therefore the method has many forms and there is your polymorphism.

Why is this useful? Because it makes your program more useful and general. Since your program doesn't care for the source of its input, it works with anything (input from a file, network, or a byte array). This is much better than writing code for all possible combinations of streams.

For more help, www.NeedProgrammingHelp.com

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.