Can't derive from class with private constructor?

K

Ken

Hi. I wanted to derive a class from a Singleton base class (the
derived class doesn't need to be a Singleton). However, I'm finding
that in Java, when I derive a class, if the constructor in the base
class is private, I get a compile error in the derived class'
constructor. I'm calling my base class Base and my derived class
Derived. Both Base and Derived are in the same package. Here's the
error text:

"Implicit super constructer Base() is not visible. Must implicilty
invoke another contstructor."

Is this a Java constraint, namely that you cannot derive a class from
a base class that has only a private constructor?

Note that I'm running Java 1.4.2 through Eclipse 3.0.

Thanks for any info,

Ken
 
J

Joona I Palaste

Ken said:
Hi. I wanted to derive a class from a Singleton base class (the
derived class doesn't need to be a Singleton). However, I'm finding
that in Java, when I derive a class, if the constructor in the base
class is private, I get a compile error in the derived class'
constructor. I'm calling my base class Base and my derived class
Derived. Both Base and Derived are in the same package. Here's the
error text:
"Implicit super constructer Base() is not visible. Must implicilty
invoke another contstructor."
Is this a Java constraint, namely that you cannot derive a class from
a base class that has only a private constructor?
Note that I'm running Java 1.4.2 through Eclipse 3.0.

AFAIK, all constructors always begin by calling a superclass
constructor. If the constructor explicitly contains a call to a
superclass constructor, that constructor is used. Otherwise the
no-argument constructor is implied. If the no-argument constructor does
not exist or is not visible to the subclass, you get a compile-time
error.
So no, I don't think it's possible to derive a subclass from a class
whose every constructor is private.
 
C

Chris Smith

Ken said:
Is this a Java constraint, namely that you cannot derive a class from
a base class that has only a private constructor?

Basically, yes. That's not actually *quite* true, since the rule in
Java is that private members are always mutually accessible from within
the same top-level class (JLS 6.6.1 for details), so the following is
the exception that proves the rule:

public class ContainingClass
{
public static class ClassA
{
private ClassA() { }
}

public static class ClassB extends ClassA
{
public ClassB() { }
}
}

This is allowed only because ClassA and ClassB are both members of
ContainingClass, and everything in ContainingClass is allowed to access
other private things that are also in ContainingClass. With top-level
classes, though, this would not be possible.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
K

kk_oop

So based on this, it would seem that by definition, Singletons in Java
are all "final" classes in that the private constructor would preclude
deriving any classes from the Singleton.

Sound right?

Thanks,

Ken
 
M

Michael Borgwardt

kk_oop said:
So based on this, it would seem that by definition, Singletons in Java
are all "final" classes in that the private constructor would preclude
deriving any classes from the Singleton.

Well, you could make it protected, but of course then other classes from
the same package could also use it.
 
J

Joona I Palaste

Well, you could make it protected, but of course then other classes from
the same package could also use it.

Which is why I think there should be a "this class and subclasses only"
access modifier in Java.

public = every class
protected = subclasses and package only
(no modifier) = package only
private = this class only

IMO there should be a "subclasses only" modifier in contrast to the
"package only" modifier.
 
R

Roedy Green

Which is why I think there should be a "this class and subclasses only"
access modifier in Java.

public = every class
protected = subclasses and package only
(no modifier) = package only
private = this class only

IMO there should be a "subclasses only" modifier in contrast to the
"package only" modifier.

How is that different from protected?

I would like an explicit word for "default" just so we can talk about
it unambiguously and so it could be explicitly stated, to make it
clear the scope was not just left out by mistake.
 
J

Joona I Palaste

Roedy Green said:
On 18 Jul 2004 19:07:12 GMT, Joona I Palaste <[email protected]>
wrote or quoted :
How is that different from protected?

Can't you read? Protected is subclasses and package only. What about
classes that are in the same package but don't inherit from the class?
I would like an explicit word for "default" just so we can talk about
it unambiguously and so it could be explicitly stated, to make it
clear the scope was not just left out by mistake.

Agreed. But it has to be some more descriptive word than "default".
 
T

Tor Iver Wilhelmsen

Joona I Palaste said:
Which is why I think there should be a "this class and subclasses only"
access modifier in Java.

There was - "private protected" - but it was abandoned a long time
ago. See if there is a JSR on the subject.

http://www.jcp.org
 
R

Roedy Green

Can't you read? Protected is subclasses and package only. What about
classes that are in the same package but don't inherit from the class?

In Java 1.0 there used to be private protected. Was that what you
wanted? I have forgotten now what it did.
 
R

Roedy Green

Can't you read?

Why the hyperbole? Why the putdown? Obviously I can read. I just
for some reason missed the point of what you were getting at.

This is normal in human conversation. All you can do is state
something in a different way when others don't get it the first time.

I thought perhaps you were asking for something that allowed
subclasses but not the class itself access, which would not make
sense.
 
R

Roedy Green

Why the hyperbole? Why the putdown? Obviously I can read. I just
for some reason missed the point of what you were getting at.

You would not be that rude face to face. Why do you think such
language is appropriate in a public forum?
 
P

Peter Ashford

Roedy said:
You would not be that rude face to face. Why do you think such
language is appropriate in a public forum?

*sigh* It seems to be endemic on usenet. I'm kinda sick of people being
so casually rude because the interface is abstract (not that I mind a
good argument, but only with people who know where you're coming from
and who are up for it)

I think that rudeness on Usenet is a sign of an inability to express
oneself and debate in a reasonable manner. It is evidence the lack of
teaching about critical thinking, IMO.
 
P

Peter Ashford

Roedy said:
How is that different from protected?

I would like an explicit word for "default" just so we can talk about
it unambiguously and so it could be explicitly stated, to make it
clear the scope was not just left out by mistake.

I agree, though I've always wanted it to be "package"
 
R

Roedy Green

I agree, though I've always wanted it to be "package"

I agree calling it literally "default" would be silly.

I think "package" would work. I don't think that would seriously
confuse a parser. Sun went to absurd lengths to avoid adding new
keywords in Java 1.5, so I think they would go with "package" if they
ever allowed this. In any case, I wish they would give the scope an
official name to use in discussion other than "default".

Ditto I would like "instance" officially designated as the opposite of
"static", if only for discussion.

If ever we start over, static should be renamed. There is nothing
static about static variables. You might call them "common" or
"shared" to emphasise they are common to all objects of the class.

The "class" keyword would not work even though that is the most
logical adjective. It already has too much meaning.
 
P

Peter Ashford

Roedy said:
I agree calling it literally "default" would be silly.

I think "package" would work. I don't think that would seriously
confuse a parser. Sun went to absurd lengths to avoid adding new
keywords in Java 1.5,

I agree with that. I really think they should have used "foreach" and
"in" for the new for loop syntax. Now we're stuck with poor syntax for
the life of Java as opposed to maybe 1% of the java programming
population having a one-off bit of extra work to refactor some code.

Having used c# for the last 6 months or so, I've come to loath it with a
passion, but the one thing I do prefer in it over java is the foreach
syntax.


so I think they would go with "package" if they
ever allowed this. In any case, I wish they would give the scope an
official name to use in discussion other than "default".

Ditto I would like "instance" officially designated as the opposite of
"static", if only for discussion.

If ever we start over, static should be renamed. There is nothing
static about static variables. You might call them "common" or
"shared" to emphasise they are common to all objects of the class.

shared would be good. static is clearly a hang over from C.
The "class" keyword would not work even though that is the most
logical adjective. It already has too much meaning.

You know, all these issues could be solved with a preprocessor :eek:)
 
R

Ron Albright

Having used c# for the last 6 months or so, I've come to loath it with a
passion, but the one thing I do prefer in it over java is the foreach
syntax.

A bit of thread drift, but I'm curious as to why you loath
it. I was looking at some C# contract work.

Ron Albright
moron(at)KILLSPAM.pobox.com
 
P

Peter Ashford

Ron said:
A bit of thread drift, but I'm curious as to why you loath
it. I was looking at some C# contract work.

Mainly problems with .NET: it's an impoverished API compared to Java.
Doesn't have simple things like a table component (DataGrid is *not*
usable for most non-db applications), doesn't have an Action component
ala Java or Delphi, doesn't have icons on menus.

Visual Studio is a PITA. It breaks when you try to inheirit non
graphical elements of a GUI component. It creates resource files which
it then gets out of sync with and can't recreate. In general, it's
buggy beta quality software.

C# itself doesn't have anonymous inner classes, doesn't yet have
anonymous methods for delegates. Inner classes can't access containing
classes non static members, delegates are ugly, properties are ugly.

Basically its Java designed by the same people who brought you Delphi
and Microsoft Bob.
 
J

Joona I Palaste

Roedy Green said:
On 18 Jul 2004 19:50:21 GMT, Joona I Palaste <[email protected]>
wrote or quoted :
Why the hyperbole? Why the putdown? Obviously I can read. I just
for some reason missed the point of what you were getting at.
This is normal in human conversation. All you can do is state
something in a different way when others don't get it the first time.
I thought perhaps you were asking for something that allowed
subclasses but not the class itself access, which would not make
sense.

OK, now I understand. Sorry I was so rude to you. It was late at night
and I had other problems in my mind too.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Outside of a dog, a book is a man's best friend. Inside a dog, it's too dark
to read anyway."
- Groucho Marx
 
I

iamfractal

Joona I Palaste said:
Which is why I think there should be a "this class and subclasses only"
access modifier in Java.

public = every class
protected = subclasses and package only

Just for completeness: protected isn't just package only. A protected
method in a public class can be overridden by subclasses in other
packages; a default-modifier method cannot.

..ed

www.EdmundKirwan.com - Home of The Fractal Class Composition
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top