permaculture playing cards*
The moose likes Beginning Java and the fly likes What is the purpose of overriding,overloading and interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the purpose of overriding,overloading and interface?" Watch "What is the purpose of overriding,overloading and interface?" New topic

What is the purpose of overriding,overloading and interface?

Jay Dilla
Ranch Hand

Joined: Aug 12, 2004
Posts: 201
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 ]
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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
[ June 18, 2007: Message edited by: Gavin Tranter ]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
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.)

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

Very nice answers. Thank you!

===Vyas Sanzgiri===
My Blog
Jay Dilla
Ranch Hand

Joined: Aug 12, 2004
Posts: 201
Great answers! Thanks guys
I agree. Here's the link:
subject: What is the purpose of overriding,overloading and interface?
Similar Threads
Question for Interviewers: How effective are textbook interview questions ?
Writing programs on interviews
Job interviews: What not to say
passes new exam with 94%
hOW IS POLYmorphism useful ?