Skip to main content

Building my own signature on class methods that don't contain all the parameters I want?!

No replies
Joined: 2005-05-12
Points: 0

I'm not a "Java guru" so I don't always know what "the accepted way" is in Java. I've been developing for almost 30 years now and can generally find my way around any language, but... I'm also "advanced" now in terms of career and don't spend a lot of time coding, so, again, I'm not up on "the accepted way" in Java.

Without writing all the specifics in Java itself, my question is this:

Suppose I have two objects Foo and Bar. Bar can hold an object instance of Foo as well as a few other private variables. Foo has private variables a, b, c, d and e. Since Foo and Bar are classes I am borrowing from another jar and I don't have the source, I have to work with the JavaDocs on this one and just go with what I am given.

So Bar has a Bar.addFoo() and signatures that accept various variations of initializing Foo, but none that accept all five variables a, b, c, d & e. Let's say Bar has private variables x, y and f where f is a Foo. And I can initialize a Bar without f or Foo and add Foo later using Bar.addFoo():

Bar bar = new Bar( 1, 2 );
bar.addFoo( new Foo( a, b, c ) );

All the available signatures for Foo are:

Foo foo = new Foo( a );
Foo foo = new Foo( a, b );
Foo foo = new Foo( a, b, c );

There are NO signatures that include d & e. I would like to be able to:

Foo foo = new Foo( a, b, c, d, e );

Correspondingly Bar.addFoo() signatures are:

bar.addFoo( new Foo( a ) ); // I can add any new Foo()
bar.addFoo( a );
bar.addFoo( a, b );
bar.addFoo( a, b, c );

I want to be able, as with a new Foo() above to be able to:

bar.addFoo( new Foo( a, b, c, d, e ) );


bar.addFoo( a, b, c, d, e );

What is the excepted way of creating my own signature that allows d & e to be initialized?

I've accomplished this in a function by doing roughly this:

import some.Foo;
import some.Bar;

public class Blah {

   public Blah() {}

   public static void BlahTest() {
      class LocalFoo extends Foo {
         public LocalFoo( int a, int b, int c, boolean d, boolean e ) {
            this.setA( a );
            this.setB( b );
            this.setC( c );
            this.setD( d );
            this.setE( e );
      Bar bar = new Bar( 1, 2 );
      // This compiles and works...
      bar.addFoo( new LocalFoo( 3, 4, 5, true, false ) );


I hope this is enough to let everyone know what I am trying to do. How do I extend the signature on a class when I don't have the source to the class? (It appears to me the design on the objects Foo and Bar isn't that great, but be that as it may... I'm stuck without the signature I want and that I know addresses the underlying attributes of the class..??!!)


PS. The caveat is, I'd rather not create an outside class either. I'm trying to keep the code inside of one Java file for various reasons. So the "local class" approach... Is this the best/only way?