This discussion is archived
1 2 3 Previous Next 43 Replies Latest reply: Jan 22, 2010 7:19 AM by 791266 RSS

force implemention of constructors ?

843798 Newbie
Currently Being Moderated
Hi,

Suppose I want an abstract class ... and suppose I would like to force
all my children to implement some constructors.

How can it be enforced ?

not like this:
abstract class Ja {
  public /* abstract */ Ja(int k){ } // abstract not allowed here.
}
although this approach will force the implementor to impl one class,
it does not enforce all constructors being implemented.

so ... can we use an interface ? no.

how to acheive this ? or its just not possible (i think so, but i also think
it would be useful).
  • 1. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    http://forum.java.sun.com/thread.jsp?forum=54&thread=333737
    http://forum.java.sun.com/thread.jsp?forum=256&thread=44913
    http://forum.java.sun.com/thread.jsp?forum=31&thread=190755

    All these threads say no.

    Why do you want to do this?

    You could use reflection to get the constructors of the class.
  • 2. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    Why do you want to do this?
    No reason, I suppose, I was just writing an abstract class and wondered
    at the possibility :)

    In truth I can't exactly think of a usefulness, at the moment, but I thought
    there may have been some interesting work-arounds or that there may
    have been a way and I didn't know it :)
  • 3. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    Hypothetically speaking, if interfaces did allow you do specify the constructor then
    given a reference to a Class object, you could test assignability to the interface,
    and if it returned true, then you could call the getConstructor(Class[]) method
    without having to do a lot of extra programming using reflection. Then again,
    you could just call the getConstructor(Class[]) method anyway and catch the
    NoSuchMethodException.

    It's probably a good thing that interfaces don't include constructor definitions,
    because the constructor descibes how the object can be created. It might
    be useful for cases with final variables. The constructor is sort of a method
    that can only be called once.

    I had a design where I required subclasses of a particular class to implement
    a constructor. However, switching to inner classes allowed the constructor
    to be replaced with the empty constructor and thus elliminated the requirement.

  • 4. Re: force implemention of constructors ?
    3004 Newbie
    Currently Being Moderated
    It's probably a good thing that interfaces don't
    include constructor definitions,
    because the constructor descibes how the object can be
    created.
    Exactly. Often a subclass needs to have additional parameters provided to the constructor to initialize state that isn't relevant for the superclass. I guess that's what it comes down to--interfaces are about behavior, but constructors are mainly about state.

    Lame-o contrived example...
    interface Employee {
        Employee(String id);
        ...
    }
    
    public class SimpleEmployee implements Employee {
        private final String id_;
        public SimpleEmployee(String id) {
            id_ = id;
        }
        ...
    }
    
    public class ForeignEmployee extends SimpleEmployee {
        private final String visaID_;
    
        public ForeignEmployee(Strind id, String visaID) {
            super(id);
            visaID_ = visaID;
        }
    
        // We have to have this ctor, because the interface says so
        public ForeignEmployee(String id) {
            super(id);
            // Uh-oh! What do we do about visaID?
            // This ctor shouldn't even be here according to the semantics of this class.
        }
        ...
    } 
    ¶
  • 5. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    Exactly.
    I am not suggesting interfaces should give this behaviour, i am
    suggesting abstract classes should.
  • 6. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    e.g:
    abstract class MoobyEngine {
         public abstract MoobyEngine(String url);
    }
    
    class Zoof extends MoobyEngine {
         //
         // Requirements:
         // ---------------------
         // Each "MoobyEngine" will set itself
         // up differently based on the url - futher,
         // MoobyEngine's cannot be created w/o a
         // url specified.
         //
         public Zoof(String url){
              //
              // Note: The super-class actually has no
              // constructor so a call to "super" is
              // not required. However it results in the
              // typicall 'non-implemented abstract method'
              // error if left out, not the typical 'extending
              // without using the right ctor' error.
              //
         }
    }
  • 7. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    Can you provide an example of how this might be used? (Contrived lame-o examples are permitted in this thread apparently :) )
  • 8. Re: force implemention of constructors ?
    3004 Newbie
    Currently Being Moderated
    Exactly.
    I am not suggesting interfaces should give this
    behaviour, i am
    suggesting abstract classes should.
    Oops. Saw "cant we use an interface" in the OP and the brain locked onto that.

    The points I made still hold though--wheter it's an interface or an abstract superclass or a concrete superclass, subclasses will have to implement the specified constructors. You still might not want them to do that, and for the same reasons. And again, constructors are mainly about state, but methods specified in an abstract class or interface are about behavior.

    JMO
  • 9. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    Can you provide an example of how this might be used?
    Did you see the one above before posting this ?
  • 10. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    wheter it's an interface or an abstract superclass
    or a concrete superclass, subclasses will have
    to implement the specified constructors. You still
    might not want them to do that, and for the same reasons.
    huh ? Lost me here.

    And again, constructors are mainly about state, but methods
    specified in an abstract class [...] are about behavior.
    I don't see your point ... so its okay to force (and hence guarantee)
    some methods to be implemented (and "behaviour") but not okay
    to do so with ctors ? Why ? Because they set the "state" of your
    object ?

    To me it seems reasonable that you, as the designer of a set of classes,
    get to determine the ways in which, at least, they must be constructed.

    Why isn't it reasonable ?
  • 11. Re: force implemention of constructors ?
    3004 Newbie
    Currently Being Moderated
    To me it seems reasonable that you, as the designer of
    a set of classes,
    get to determine the ways in which, at least, they
    must be constructed.
    It's okay for me to say "implementing classes / subclasses must present at least this public interface." However, if I enforce a constructor, I'm effectively shutting out all potential subclasses that might have additional state requirements beyond what that constructor says. If I'm required to provide a constructor that takes only A and B, and I want to provide a subclass that also requires a C at construction time in order to have valid state, I can't do it.

    ¶
  • 12. Re: force implemention of constructors ?
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    I suppose it is simply a matter of tradeoffs.

    You can't have a relatively simple language that also provides a lot of power and yet provides a complete set of protection mechanisms for all types of usage.
  • 13. Re: force implemention of constructors ?
    3004 Newbie
    Currently Being Moderated
    wheter it's an interface or an abstract superclass
    or a concrete superclass, subclasses will have
    to implement the specified constructors. You still
    might not want them to do that, and for the same
    reasons.

    huh ? Lost me here.
    Just like in my example with interfaces--if you require a subclass to support a particular constructor, you may be preventing it from fully initializing its state.

    And again, constructors are mainly about state, but
    methods
    specified in an abstract class [...] are about
    behavior.

    I don't see your point ... so its okay to force (and
    hence guarantee)
    some methods to be implemented (and "behaviour") but
    not okay
    to do so with ctors ? Why ? Because they set the
    "state" of your
    object ?
    The point of my state vs. behavior comment was to try to find a justification for why constructors might be different from methods. I realize it begs the question "why should be able to enforce behavior, but not state?" but I was just trying to show that you can make the argument that constructors are fundamentally different from methods in at least one respect, and therefore it makes sense that the two wouldn't necessarily be symmetric with regard to their applicability in inheritance.

    As for "why is okay to enforce behavior but not state?" I don't really have a good answer. One thing that pops to mind is that sure, in theory maybe there's nothing wrong with wanting to enforce state, but the problem with actually impelementing that enforcement is, as I pointed out, you may be cheating your subclasses out of some state that they need.

    I guess if you go back to the purpose of inheritance--the is-a relationship (oh, god, please, not again!) that lets you substitute a subclass wherever a superclass (or interface is expected), then constructors don't really fit into that. You have a method that requires a certain type as an arg, or assignment expression that's expecting a certain type. Your class implements the interface or extends the class so you can use it in that context, not so that you can construct it a certain way.

    I realize the above paragraph is pretty mushy. I can't offer anything more concrete than that, but I think it makes sense.

    ¶
  • 14. Re: force implemention of constructors ?
    3004 Newbie
    Currently Being Moderated
    I suppose it is simply a matter of tradeoffs.

    You can't have a relatively simple language that also
    provides a lot of power and yet provides a complete
    set of protection mechanisms for all types of usage.
    You're just trying to confuse me.

    UJ! Help!

    ;-)

    ¶
1 2 3 Previous Next