• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Should 1 overloaded method call the other to avoid redundant code ?

 
Ranch Hand
Posts: 1510
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes, a large part of the work two overloaded methods are doing is same and only that the second method takes an additional parameter and has slight change in code. Should the first method call the second one in this case to avoid redundancy of code? If so , how ?

Example

Instead of doing as above can the second method call the first one and use it to reduce redundancy of code  ?

Thanks
 
Marshal
Posts: 69809
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
 
Monica Shiralkar
Ranch Hand
Posts: 1510
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks , In the below code can I call the first method from second one without providing the argument z and instead writing a code which provides default value to the method call in such a case when no parameter is supplied ?


 
Campbell Ritchie
Marshal
Posts: 69809
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Saloon Keeper
Posts: 12142
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two basic scenarios: Either the method that has the extra parameter performs a generalized algorithm and the other method represents a specific common case; or the method that has the extra parameter performs additional processing that is not present in the other method call.

In the first scenario you can call the generalized method from your specific case. It might look like this:

As you can see, it works just like a regular method call.

The second scenario doesn't happen that often. It's usually a sign that the method with the extra parameter does too much and should be split in smaller methods. It's more common with constructors though, where one constructor will initialize part of the fields and the constructor with additional parameters will initialize additional fields:
 
Monica Shiralkar
Ranch Hand
Posts: 1510
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What happened when you tried it?



It gave a syntax error as I was trying something like this which gave syntax error:

I was trying something but realised that default parameters are not suported .
 
Saloon Keeper
Posts: 22265
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a fine old tradition with a lot of utility.

Another form is when you have sub-classing and you want to wrap the base class method with additional functionality:
 
Campbell Ritchie
Marshal
Posts: 69809
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:. . . syntax error: . . .

Good grief! How did you think that code would ever compile?

default parameters are not suported .

What's a default parameter? Please explain what its use would be.
 
Monica Shiralkar
Ranch Hand
Posts: 1510
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What's a default parameter? Please explain what its use would be.



The default parameter for a method has a default value to be used when no value for that parameter is supplied . If there is a default  parameter z =true , then if no argument is supplied it would take value of z to be true else if value is supplied it will take that value. But such a feature is not there ,so what is the way to achieve the same .
 
Tim Holloway
Saloon Keeper
Posts: 22265
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:

Campbell Ritchie wrote:What's a default parameter? Please explain what its use would be.



The default parameter for a method has a default value to be used when no value for that parameter is supplied . If there is a default  parameter z =true , then if no argument is supplied it would take value of z to be true else if value is supplied it will take that value. But such a feature is not there ,so what is the way to achieve the same .



Incidentally, while it is a nuisance to have to write multiple methods to achieve the effect of default parameters in Java, it's an absolute nightmare for compiler designers to support default parameters.

Not because it's hard to do default parameter value assignment, but because it immensely complicates the task of matching one of many method prototypes with a method call.

I know, I tried back when I was attempting to implement a native C++ compiler.
 
Monica Shiralkar
Ranch Hand
Posts: 1510
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

The second scenario doesn't happen that often. It's usually a sign that the method with the extra parameter does too much and should be split in smaller methods.
 



And what if it is a method from a Java library that we are calling ?
Thanks .
 
Stephan van Hulst
Saloon Keeper
Posts: 12142
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What difference does it make?
 
grapes are vegan food pellets. Eat this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic