Defining c'tor within interface and abstract class

Discussion in 'Java' started by Raffael Vogler, Nov 26, 2003.

  1. Hello,
    does someone know how to define
    a c'tor within an IF and an AC?
    best regards
    Raffael Vogler, Nov 26, 2003
    #1
    1. Advertising

  2. Raffael Vogler wrote:
    > does someone know how to define
    > a c'tor within an IF and an AC?


    If you mean "constructor", then it's of course impossible in an interface,
    whereas in an abstract class it's done in exactly the same way as in
    a regular class.
    Michael Borgwardt, Nov 26, 2003
    #2
    1. Advertising

  3. Raffael Vogler

    Chris Uppal Guest

    Michael Borgwardt wrote:

    > > does someone know how to define
    > > a c'tor within an IF and an AC?

    >
    > If you mean "constructor", then it's of course impossible in an interface,


    Though you _can_ include a factory method as part of an interface; which may or
    may not be relevant, depending on what Raffael is trying to do.

    -- chris
    Chris Uppal, Nov 26, 2003
    #3
  4. Chris Uppal wrote:
    >>If you mean "constructor", then it's of course impossible in an interface,

    >
    >
    > Though you _can_ include a factory method as part of an interface;


    Except that the methods in an interface can't be static, a static method
    can't implement an interface method, and a non-static factory method
    is kinda pointless.
    Michael Borgwardt, Nov 26, 2003
    #4
  5. Michael Borgwardt <> scribbled the following:
    > Chris Uppal wrote:
    >>>If you mean "constructor", then it's of course impossible in an interface,

    >>
    >> Though you _can_ include a factory method as part of an interface;


    > Except that the methods in an interface can't be static, a static method
    > can't implement an interface method, and a non-static factory method
    > is kinda pointless.


    Not if the factory method appears in a singleton object.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "This isn't right. This isn't even wrong."
    - Wolfgang Pauli
    Joona I Palaste, Nov 27, 2003
    #5
  6. Raffael Vogler

    Chris Uppal Guest

    Michael Borgwardt wrote:

    > > Though you _can_ include a factory method as part of an interface;

    >
    > Except that the methods in an interface can't be static, a static method
    > can't implement an interface method, and a non-static factory method
    > is kinda pointless.


    Not at all ;-)

    public interface Foo { ... }

    public interface FooFactory
    {
    Foo makeFoo();
    }

    -------------

    class MyFoo extends Foo { ... }

    class MyFooFactory extends FooFactory
    {
    Foo makeFoo()
    {
    return new MyFoo();
    }
    }

    -------------

    class OtherCode
    {
    private /* maybe static */ FooFactory fooBuilder;

    // somewhere, maybe in constructor
    fooBuilder = // ...something...

    void doSomeWorkThatNeedsAFoo()
    {
    Foo foo = fooBuilder.makeFoo();
    ....
    }
    }

    -------------


    It's a pattern I use every day (since I work mostly in Smalltalk).

    Perhaps you meant that the pattern would be called 'factory object' rather than
    'factory method' ? If so then I'd agree that that distinction is sometimes
    made, but I consider it artificial and unhelpful.

    -- chris
    Chris Uppal, Nov 27, 2003
    #6
  7. Chris Uppal <-this.org> scribbled the following:
    > Michael Borgwardt wrote:
    >> > Though you _can_ include a factory method as part of an interface;

    >>
    >> Except that the methods in an interface can't be static, a static method
    >> can't implement an interface method, and a non-static factory method
    >> is kinda pointless.


    > Not at all ;-)


    > public interface Foo { ... }


    > public interface FooFactory
    > {
    > Foo makeFoo();
    > }


    > -------------


    > class MyFoo extends Foo { ... }


    > class MyFooFactory extends FooFactory


    ITYM "implements FooFactory"?

    > {
    > Foo makeFoo()
    > {
    > return new MyFoo();
    > }
    > }


    > -------------


    > class OtherCode
    > {
    > private /* maybe static */ FooFactory fooBuilder;


    > // somewhere, maybe in constructor
    > fooBuilder = // ...something...


    > void doSomeWorkThatNeedsAFoo()
    > {
    > Foo foo = fooBuilder.makeFoo();
    > ....
    > }
    > }


    > -------------



    > It's a pattern I use every day (since I work mostly in Smalltalk).


    It's also used rather extensively in the JAXP (Java API for XML
    Parsing) framework.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Shh! The maestro is decomposing!"
    - Gary Larson
    Joona I Palaste, Nov 27, 2003
    #7
  8. Raffael Vogler wrote:

    > Hello,
    > does someone know how to define
    > a c'tor within an IF and an AC?


    You can't specify a constructor in an interface. As for abstract classes,
    they're just classes which cannot be instantiated, so you would define a
    constructor just as you would any other class.

    Why do you want to specify a constructor in an interface, if you don't mind
    answering?

    --
    Darryl L. Pierce <>
    Visit the Infobahn Offramp - <http://mypage.org/mcpierce>
    "What do you care what other people think, Mr. Feynman?"
    Darryl L. Pierce, Nov 30, 2003
    #8
  9. Raffael Vogler

    Chris Uppal Guest

    Joona I Palaste wrote:
    > Chris Uppal <-this.org> scribbled the


    > > class MyFooFactory extends FooFactory

    >
    > ITYM "implements FooFactory"?


    Yes, of course. Ta.


    > > It's a pattern I use every day (since I work mostly in Smalltalk).

    >
    > It's also used rather extensively in the JAXP (Java API for XML
    > Parsing) framework.


    It seems to have become quite fashionable at Java central -- most of the new
    APIs (those that I've bothered to look at) use it.

    <OT reflection>
    Further evidence, I suppose, that static typing isn't all it's cracked up to
    be...
    </OT reflection>

    -- chris
    Chris Uppal, Nov 30, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler
    Replies:
    1
    Views:
    438
    R.F. Pels
    May 22, 2005
  2. Sameer
    Replies:
    4
    Views:
    601
    Roedy Green
    Aug 31, 2005
  3. Uzytkownik
    Replies:
    3
    Views:
    592
    Uzytkownik
    Apr 3, 2005
  4. Replies:
    8
    Views:
    535
  5. Replies:
    4
    Views:
    813
    Rolf Magnus
    May 17, 2006
Loading...

Share This Page