SCJP 1.5, SCWCD 1.5
Well, required is a bit strong but overloading is one the more powerful and useful features of Object Oriented programing.Varun Nayudu wrote:Hi,
I want to ask this simple question
1>What is the need for overloading method?
2> Why cant we create a different method instead of overloading the method?
It's not what your program can do, it's what your users do with the program.
Joe Areeda wrote:
Well, required is a bit strong but overloading is one the more powerful and useful features of Object Oriented programing.Varun Nayudu wrote:Hi,
I want to ask this simple question
1>What is the need for overloading method?
2> Why cant we create a different method instead of overloading the method?
For example if we have a base class of People with an abstract method called beNice() and 2 derived classes Boys and Girls. The calling routine only needs to know about People and always calls beNice(). If the People happens to be a Girl it expresses it's feelings for hours and watches bad movies with them, if it's a Boy it brings beer. If you had separate methods you'd have to a) know about all the derived classes and b) determine which method to call
Now if later on someones adds Dogs (dogs are people too) all they have to do is implement the abstract methods, they don't have to go find all the places beNice() is called and add another test of what it is and call the appropriate method those places still call beNice() and the dog gets a treat and a pat on the head.
Hope that helps and the example isn't too silly.
Joe
luck, db
There are no new questions, but there may be new answers.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Oops that's what happens when I start typing before I start thinking.Darryl Burke wrote:
That's overriding, not overloading.
It's not what your program can do, it's what your users do with the program.
SCJP 1.5, SCWCD 1.5
Varun Nayudu wrote:But there might be something more to overloading than just to keep the method name's similar so that we dont have to remember methods.
Matthew Brown wrote:If you've used a language without overloading, have you never thought "you know, I really wish I could just reuse that method name with different arguments". I have - loads of times. The people that designed Java probably had as well.
Rob Spoor wrote:
Overloading isn't new to Java. Languages like C++ and Delphi also have it. Since Java is based on C++ that's probably why Java has it in the first place.
Quick! Before anybody notices! Cover it up with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|