Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

method signatures / overloading / overriding 2

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
0
0
GB
Hi,

I've started to learn more about overriding and overloading.

I think I understand how they work and I understand what makes a method signature.

However, what I don't understand is the point, or the usefullness. I cannot see the advantages, and hoped someone might clear things up...

As methods are executed by Java based on their message signature, if I want to add an argument to a method, this changes its signature, so now any other objects which use the modified method would break, as the signaure has changed and the message sends are no longer valid.

How is that helpfull or usefull. All other languages I have used you have one function/subroutine and the arguments can be optional, which makes things nice and simple and easy to work with.

In Java however if you want to alter a method and add a new parameter, without breaking other classes using that method, you have to overload the method, by creating another one which allows the new argument creating a new method signature.

So now you have to duplicate code that's already been written, just so I can add a new paramerter and a few extra lines of code to a method. sounds crazy to me, where is 'code reuse' practices there?

On top of this, if I want to override a method, and the programmer has writen 3 method signatures to a specific method name I now have to create 3 method signatures to override them, yet again causing code bloat and not following 'code reuse' practices.

Perhaps I'm missing something, or have missunderstood the course material, but if I understand method signatures, overriding and overloading, in terms of the way it works, then I don't get it, I find it cumbersome and could be a real nightmare to work with.

So any advice unravelling the usefullness and purpose of this behaviour is much appreciated.

1DMF.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> PERL v1.5 beta (FusionCharts)
 
If you want to reuse the original method code you don't need to duplicate it, you can call the original one from the new method.
If you have:
Code:
public void method(String param1)
{
    //do something;
}
and you want to add some extra feature with a second param you can:
Code:
public void method(String param1)
{
    //do something with param1
}
public void method(String param1, String param2)
{
    this.method(param1);  //reuses the original code
    //new code - do something with param2
}

If a method is already overloaded and has several signatures (for example 3 as you said), you don't need to add 3 new signatures. You only need to add a new method signature with the behaviour you need.

If you have:
method(A)
method(A,B)
method(A,B,C)
and you need a method that uses a fourth parameter you only need to add method(A,B,C,D).

But if you need all the method signatures to be able to accept a new param, in this case yes, you need to add 3 new signatures method(A,D), method(A,B,D), method(A,B,C,D).

But in fact, you usually don't need to overload methods very often. If you're doing it a lot perhaps you'll need to create new methods, instead of overloading the existing ones.
What you'll mostly need is to override or to overload an inherited method when your class extends another class.
 
Thanks Morefeo,

Looks like i do understand overridding / overloading and signatures.

I appreciate that we are mainly talking about sub-classes, when overriding, or perhaps overloading is required, but technically all objects are made up of sub-classes.

As java.lang.object will always be the super-class of all objects won't it?

To me having to create 3 new methods, and potentially duplicate the code from the super-class into your new method signatues, is just code bloat, inefficient and a down right pain in the ass for a programmer.

OK, I appreciate the pseudo variable 'super' can be used to execute the super-class method, but what if the class you are extending has a whole load of protocol methods you don't want your object to have or at least not public which they may be in the super-class.

does that now mean you have to create a whole bunch of superfluous signatures just to override the super-class method, setting the access modifier of the sub-class method
to 'private' which you would then execute the super-class method using super.methodName, thus now making some inherited protocols 'hidden' for your sub-class object instance.

Not to mention if you wanted to create a new signature for a method ... method(A,B,C,D).

And for your object to operate in the desired manner, it cannot be allowed to use any of the inherited methods in its protocol of method(A), method(A,B), method(A,B,C).

Do you now have to create overriding method signatures that do nothing just to remove them from the sub-classes protocol?

Perhaps there is a mechanism for blocking unwanted inherited methods, that hasn't been covered in the course yet, so I apologise if that is the case, but it seems to me if you have to use overloading / overrriding it is completelty obstructive to ease of deveopment or code reuse priciples.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> PERL v1.5 beta (FusionCharts)
 
If you want to extend a class with a lot of methods you don't want to have, maybe you should not extend it but write a wrapper.

I like the overloading thinguie. That way you can choose which methods you like. With optional parameters, you would need to write a lot of ifs, and that's very difficult to maintain.

Cheers,
Dian
 
If you want to extend a class with a lot of methods you don't want to have, maybe you should not extend it but write a wrapper.
I thought there must be a better way. 'wrapper' is not something I have come across yet. So I hope this is covered later on in the course.


Also bear in mind, this is all new to me and is theoretical, it's not like I’ve been writing a lot of classes or sub-classes.

I'm just trying to understand how it works and why you would do it, as it seems a bit alien at first.

A lot of nested if's my not be required, if it's just a few extra parameters, perhaps a switch/case type construct would be better.

I guess knowing how to override/overload is just part of the puzzle, it's knowing 'when', that's the key.

I guess that comes with experience and practice, as in a theoretical world of just 'concept' it becomes a bit confusing and overwhelming, well at first to me it has.

Also I suppose having coded for so many years in a procedural construct, trying to shake of old habits and concepts is hard.

But I will do my best to persevere.

Thanks for your input :)


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> PERL v1.5 beta (FusionCharts)
 
Once you start coding you'll see that this overloading/overriding doesn't cause much trouble, and you usually won't have this kind of dilemas.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top