i know how do them and the rules that apply to them,etc. But I can't articulate their real purpose in applications. Can someone explain with real world examples of why you would use overriding, overloading and interfaces in your applications? I had an interview where the guy asked me about overriding and I answered what it was and show him an example of how you would write it out, then he asked me when would you use it....and I had no idea. So after a few more questions which he also confirmed I answered correct, he said they needed someone will more experience and that was that. I guess it was a classic example of a person with no experience knowing all the definitions and concepts but having no idea how or when they are actually used. So can someone help me here, I have another interview coming up. [ June 18, 2007: Message edited by: Jay Dilla ]
overriding would be used to change how a sub-class implements an inherited behaviour, you do this when you implement (override) toString() to change what the object will "print" when its toString is called. Same for hashCode so you can define how an object will calculate its hashValue, and equals to determine how one object determines its equality with another.
overloading is simply to provide several to call the same method but with different paremters, often used with Constructors. Say you had a method to compute the fibonarcci (spelling?) sequence, you could provide a method that defines what number to start computing at called compute(int start) and a second that also allows you to define how many iterations you wish to compute, compute(int start, int interations).
This might look like:
Interface can be used to define a behaviour that cuts across several inheritance trees, so rather then all objects that need to Bounce inheriting from a single Bounce object, they could implement a Bounce interface and inherit from their natural parent object. They can be used to define a contract/API, client classes work to the API rather then work to a given implementation.
Am sure there or other reasons, and vastly better explenations (mostly spelt better too )
Its probably worth checking out something like the Head First Java book or a book on Java certification.
Hope that helps G [ June 18, 2007: Message edited by: Gavin Tranter ]
For a very good explanation of polymorphism and overriding, including why one might use them, take a look at "How my Dog learned Polymorphism" in the JavaRanch Campfire Stories. (Note that the "Animal" parent class could just as easily be an interface, with the "Dog" class then implementing the interface, instead of extending the parent class.)