This discussion is archived
1 2 3 Previous Next 43 Replies Latest reply: Jan 22, 2010 7:19 AM by 791266 Go to original post RSS
  • 15. 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.
    You're just trying to confuse me.

    UJ! Help!
    Just trying to annoy me right? :D
  • 16. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    I did not read the whole track. But you need
    some information about Constructors, if
    your are working with real generics.

    Example: if you have a type variable T and
    you want to construct an instance of it. How
    do you know beforehand, which constructors
    the instantation type of T is offering. You have
    to bind this information somehow to T, so
    you may call new T() or new T(any args).
    Reflection is of now help as at time of writing
    the generic code with T no one knows.

  • 17. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    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.
    Well, if it were a normal inheritance example you would
    simply put in the call to super.

    In this case, the design is saying "no-one can have instances
    of you unless they pass, at least, an "A" or a "B" in." so infact,
    the behaviour you're describe is the behaviour i'm after - i.e. the
    user implements an "A" and "B" methods, as well as then implementing
    a "C" method in any fashion you see fit.
  • 18. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    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.
    See above, this is the desired behaviour, i.e:
    "You can only exist if you are created in, at least, this fashion"

    with regard to their
    applicability in inheritance.
    Well, I'll address this in a minute, but I think that my suggestion
    here is not really about inheritence of constructors - only about
    specification of constructors.

    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.
    I agree. Constructors shouldn't be inherited, and they aren't.

    All i'm saying is that with an abstract class we get to suggest
    what methods are implemented (these "abstract methods" aren't
    inherited either, just "required to be implemented" - i am only
    asking for this same treatment. :)
  • 19. Re: force implemention of constructors ?
    843798 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.
    I suppose ... but there must have been (or at least I hope there was) a
    good reason for leaving it out, and I'm just wondering at what it could
    be ...
  • 20. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    and lets not forget that abstract classes already do this
    for one constructor ... the problem is that you can't
    do it for more then one, and require that each one
    is implemented.
  • 21. Re: force implemention of constructors ?
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    and lets not forget that abstract classes already do this
    for one constructor ... the problem is that you can't
    do it for more then one, and require that each one
    is implemented.
    Abstract doesn't have anything to do with that though.


  • 22. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    and lets not forget that abstract classes already do
    this
    for one constructor ... the problem is that
    you can't
    do it for more then one, and require that each one
    is implemented.
    Abstract doesn't have anything to do with that though.
    Why not ? (you mean the abstract keyword ?) when used on
    methods with the same name, it doesn't mean "please implement
    one" it means, "please implement all" - why should it be different
    for constructors ?
  • 23. Re: force implemention of constructors ?
    3004 Newbie
    Currently Being Moderated
    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.
    Well, if it were a normal inheritance example you
    would
    simply put in the call to super.
    No.

    First, let me make sure I understand. You're talking about constructor signature could be specified as required, like regular method signatures in interfaces or abstract classes, right? And the subclass would have to implement said constructor, right?

    So I specify a constructor that takes A and B as its args. The concrete subclass (Base) implements that. Now I create a subclass of the subclass (Derived extends Base). If I understand what you're saying, Derived would also have to have said constructor.

    But what if Derived has additional state--one more member variable--and the semantics of the class require that to be specified at construction time? I can't fulfill both the ctor signature requirements of the abstract class and the semantic requirements of Derived. If I provide a ctor on Derived that takes only A and B, then Derived's C field won't have a value (assume there's no reasonable default). Sure, Derived's (A,B,C) ctor can call super(A,B), but that won't satisfy the contract requirements you've specified.

    >
    In this case, the design is saying "no-one can have
    instances
    of you unless they pass, at least, an "A" or a
    "B" in." so infact,
    the behaviour you're describe is the behaviour i'm
    after - i.e. the
    user implements an "A" and "B" methods, as well as
    then implementing
    a "C" method in any fashion you see fit.
    Eh? Methods? I thought A and B were ctor args.

    So you're saying that, contrary to what I said above, Derived(A,B,C) would satisfy the contract requirements because it has an A and a B, even though the signature doesn't match exactly? I guess that might be one way around that limitation, but it smells awfully kludgey.

    ¶

  • 24. Re: force implemention of constructors ?
    3004 Newbie
    Currently Being Moderated
    and lets not forget that abstract classes already
    do
    this
    for one constructor ... the problem is that
    you can't
    do it for more then one, and require that each one
    is implemented.
    Abstract doesn't have anything to do with that
    though.
    Silk, are you talking about the default constructor? As jschell said, that has nothing to do with abstract classes--default constructor rules are the same for abstract and concrete classes. And in any case I don't see how that matches what you're talking about. Nothing says a class has to have a no-arg constructor, regardless of whether it's abstract or not.

    Or were you talking about something else?

    >
    Why not ? (you mean the abstract keyword ?) when used
    on
    methods with the same name, it doesn't mean "please
    implement
    one" it means, "please implement all" - why should it
    be different
    for constructors ?
    Well, for one, like I said about derived classes that take more args. Now, if you go with "at least this many args," then that may solve that problem (or maybe not--I haven't given it a lot of thought), but then you're introducing more complex asymmetry between constructors and methods when, according to the above, you seem to be arguing in favor of symmetry.

    ¶
  • 25. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    No.
    Yes.

    Yes you would, unless you had already put a call to super somewhere else:
    class Super {
      public Super(int k){ }
    } 
    class Sonny extends Super {
      public Sonny(String newArg){ }
    }
    can't compile Sonny w/o putting call to super, i'm sure you don't
    disagree, but just to clarify what i was speaking about.

    Eh? Methods? I thought A and B were ctor args.
    Oh, i thought they were seperate Constructors ... :)
  • 26. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    Well, for one, like I said about derived classes that take more args.
    Now, if you go with "at least this many args,"
    No, I meant "at least this many Constructors" :)
  • 27. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    Or were you talking about something else?
    Yes.

    Okay, let me write some code, so-as to avoid confusion:

    Current behaviour:
    abstract class Engine {
         public Engine(String url){ }
         public Engine(java.io.File directory){ }
    }
    
    
    class AnalysingEngine extends Engine {
         /**
          * -Required- to implement at least one of
          * of the constructors.
          */
         public AnalysingEngine(String url){
              super(url);
         }
    
         /**
          * No requirement to implement "Engine(java.io.File)"
          */
    }
    Desired behaviour:
    abstract class Engine {
         public abstract Engine(String url);
         public abstract Engine(java.io.File directory);
    }
    
    
    class AnalysingEngine extends Engine {
         public AnalysingEngine(String url){
              /**
               * Note the abscense of "super(url);".
               */
         }
    
         /**
          * Compile-time error if left out.
          */
         public AnalysingEngine(java.io.File directory){
         }
    }
    get it ? :)
  • 28. Re: force implemention of constructors ?
    843798 Newbie
    Currently Being Moderated
    Oh, i thought they were seperate Constructors ... :)
    let me clarify.

    I thought A and B were args for seperate constructors, and where
    I used them, I was talking about them as Contructors, not as arguments :)
  • 29. Re: force implemention of constructors ?
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    and lets not forget that abstract classes already do this
    for one constructor ... the problem is that you can't
    do it for more then one, and require that each one
    is implemented.
    Abstract doesn't have anything to do with that though.
    Why not ?
    Because it applies to non-abstract classes as well - right?