Defining c'tor within interface and abstract class

R

Raffael Vogler

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

Michael Borgwardt

Raffael said:
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.
 
C

Chris Uppal

Michael said:
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
 
M

Michael Borgwardt

Chris said:
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.
 
J

Joona I Palaste

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.
 
C

Chris Uppal

Michael said:
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
 
J

Joona I Palaste

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.
 
D

Darryl L. Pierce

Raffael said:
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?
 
C

Chris Uppal

Joona said:
Chris Uppal <[email protected]> scribbled the

ITYM "implements FooFactory"?

Yes, of course. Ta.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top