Re: Forcing sub-classes to implement Constructor

Brian Maso <[email protected]>
Newsgroups gmane.comp.windows.devel.java.advanced
Message-ID <20070809152147.EVOB1335.fed1rmmtao106.cox.net@fed1rmimpo01.cox.net>
At 08:14 AM 8/9/2007, you wrote:
>Brian Maso wrote:
>>No.
>>
>>Constructors are more like static methods than regular methods: they
>>can't be declared abstract and thus force subclasses to have them. You
>>can add tools to your build to recognize certain annotations that
>>trigger verifier rules. For example, you could define an annotation
>>such as:
>
>Hmm. Brian appears to be correct after a quick test.
>
>Whats weird is that I *know* I've seen situations where the definition
>of a super class contrutctir forced the definition of sub class one...

A subclass constructor always *must* invoke a 
superclass constructor. If you have a superclass 
with only non-default constructors visible to a 
subclass, then the subclass cannot rely on the 
compiler-provided default constructor and must 
instead explicitly define a constructor that 
explicitly invokes a superclass constructor. For example:

public class Base {
   //...only public constructor...
   public Base(int x) {
     ...
   }
   ...
}


public class Sub extends Base {
   //...must explicitly class superclass constructor...
   public Sub() {
     super(0);
     ...
   }
   ...

   //...more typically, subclasses will have c-tors with same signature as
   //   superclass and just pass-through the c-tor args, like so...
   public Sub(int x) {
     super(x);
   }
   ...
}

Brian Maso



>JK
>>
>>@SubclassConstructorRequiredPattern(presense=REQUIRED,
>>constructor=PUBLIC_DEFAULT)
>>
>>which would be applied to the base class. Some part of your build then
>>would scan code either jsut before or just after compilation to see if
>>it breaks all the rules defined by such annotations.
>>
>>Sounds like an awful lot of work, though. What's your use-case? Why do
>>you ned subclasses to supply a public default constructor?
>>
>>Brian Maso
>>
>>At 07:13 AM 8/9/2007, you wrote:
>>>Is there a way to force subclasses of a class to implement a certain
>>>(read
>>>empty) constructor.
>>>
>>>mvh roger j
>>>
>>>===================================
>>>This list is hosted by DevelopMentor®  http://www.develop.com
>>>
>>>View archives and manage your subscription(s) at
>>>http://discuss.develop.com
>>
>>===================================
>>This list is hosted by DevelopMentor®  http://www.develop.com
>>
>>View archives and manage your subscription(s) at
>>http://discuss.develop.com
>
>===================================
>This list is hosted by DevelopMentor®  http://www.develop.com
>
>View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.