Imagine you program a small mini framework. The code works. Now one of the clients decides to overwrite some behavior here and there. This is fine since you old code would simply cast it's way up to the concrete class that was assigned to the super type.
This is called polymorphism and is one of the core concepts of Java. It keeps code extensible.
Also, there is a principle that says 'Program to an Interface, not to an Implementation'
JDBCSupport - An easy to use, light-weight JDBC framework -
Welcome to JavaRanch. Please check your private messages for an administrative matter from JavaRanch.
About your question: It is a good design principle to program to an interface, not an implementation. Programming to an interface, instead of a specific implementation of the interface, decouples your code from that specific implementation. Look at this example:
Suppose that sometime later you discover that in this particular case, it would be better to use a LinkedList instead of an ArrayList. Now you can just change the code to this:
And you're done. The rest of your program only needs to know that data is a List of String objects - it does not need to know what implementation of interface List is used.
Simple thing you need to get here is polymorphism helps in reassigning something else later on without actually modufying the entire code.
anime A = new horse() and later on you want to use dog as an animal as you found horse is too big and its not that friendly too so you bring a dog and assign it to your animal designation animal A = new dog(). with horse h = new horse(), h will complain if you try to bring in a dog and assign it to the dog as it was not compatible to hold the dog and assume they have some enmity between them
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
for small projects where one person usually writes all the code, it is often hard to make a solid case.
but if one is selling code to dozens or hundreds of customers, you have options:
1) tell them you'll give them an animal.
2) tell them you'll give them a horse.
Let's assume you do option 2. They write thousand and thousands of lines of code, each time explicitly naming the Horse class. Now for some reason, you decide that to improve your code. You may not want to always return a horse, but maybe a mule, or a llama, or some other pack animal that may work better in some cases.
All your customers have to review all their code, looking for any place they have a Horse reference, and re-code.
Now let's assume I tell my customers I'll give them an animal. at first, the only animal I can return is a Horse, but they don't need to know that. So, they write all their code using an Animal reference. When I decide to start returning llamas sometimes, my customers don't need to change a thing.
all your customers get mad at your design, that when they re-factor their code, they do it to use MY package. all your customers are gone, and my customer base has doubled.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors