Java subclass: several constructors inherited from an abstract superclass

Basically, I have:

public abstract class AbstractClass {
    public AbstractClass( Type arg0, Type arg1, Type arg2 ) {
        // do some stuff with all those args
    }

    public AbstractClass( Type onlyOneArg ) {
        // do different stuffs with this different arg.
    }

    protected someMethods() { /* ... */ }
}

And I have several problems in subclasses:

  • First, in most cases, I should uselessly rewrite the constructors. Not very annoying, just a little dirty for the eyes.
  • And, more important , I am not forced to implement both constructors (although both of them are used in the program).

An example of my current subclasses:

public class MyClass extends AbstractClass {
    public MyClass( Type arg0, Type arg1, Type arg2 ) {
        super( arg0, arg1, arg2 );
    }

    public MyClass( Type onlyOneArg ) {
        super( onlyOneArg );
    }
}

and

  • I should be able to write some specific code in the constructor of the subclass if I want.
  • I have too much common code that I want to keep in an abstract class.

Can i do something? Is there something I don't know about Java? Or is my design bad? Or..?

+5
source share
2

( , , ) ; .

, , , .

, , (, 2 ), , , .

, , "" factory; factory . :

interface AbstractClassFactory {
  AbstractClass create( Type arg0, Type arg1, Type arg2 );
  AbstractClass create( Type onlyOneArg );
}
+1

Java. , . , , . " , ". ? , , "". , .

0

All Articles