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; 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
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...
Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...
Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...