2

This question is mainly in reference to Luiggi's answer to this SO question: Why can you not inherit from a class whose constructor is private?

I understand that Java enforces that every subclass constructor must call one of its superclass's constructors. If all the superclass's constructors are private, this is obviously not possible. So, if a subclass theoretically could inherit from a superclass with private constructors, the result would be that you couldn't call a constructor on the subclass.

But what if I never intend to create an instance of the subclass anyway? For example, what if my subclass only adds static fields and methods, and I'm only interested in using the static fields and methods of the superclass? Then I don't need a constructor for the subclass.

Community
  • 1
  • 1
JamesGold
  • 795
  • 2
  • 8
  • 24
  • So are you asking why the Java designers made the decision to disallow this sort of subclass? Or are you asking us to show you where in the JLS that this is behavior is required of compilers? Or ??? – Erick G. Hagstrom Jul 06 '15 at 00:18
  • I'm asking the first one. – JamesGold Jul 06 '15 at 00:20
  • Then it's not really a 'class' in OOP jargon. It should be a "namespace" or something. Unfortunately in Java we must use class to do it (or interface too in java8); it's a hack. – ZhongYu Jul 06 '15 at 01:49

4 Answers4

5

what if my subclass only adds static fields and methods, and I'm only interested in using the static fields and methods of the superclass

In that case you don't need inheritance - use composition!

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

You should seal your class by declaring it as final. Then it is guaranteed that no sub-classes can be made

Michael
  • 2,673
  • 1
  • 16
  • 27
  • Though what you wrote is true - it answers a totally different question than the one that was asked... – Nir Alfasi Jul 06 '15 at 00:00
  • @alfasin "But what if I never intend to construct a subclass anyway?" I thought he was saying he didn't want his class to be extended. – Michael Jul 06 '15 at 00:02
  • 1
    @Michael that's not what he says (that he didn't want his class to be extended) -in fact, the class he wants to inherit could be written by someone else - that is not part of the discussion. He asks why he can't extend a class that all its constructors are declared as private even if he doesn't intent to instantiate objects from the child class. So the question is about Java architecture that blocks him from inheriting such a class. – Nir Alfasi Jul 06 '15 at 00:06
0

If only adding subclasses and can only create the parent class, the child "class" is really just a helper class without adding any functionality/responsibilites/etc. to the parent. In many respects, it's meaningless.

Scott Sosna
  • 1,443
  • 1
  • 8
  • 8
0

A subclass of this sort would not be a legitimate subclass. Even if all of its fields and methods were declared static, it would inherit all of the fields and methods of all of its superclasses, all the way back up to Object. And there are non-static methods in Object. So this subclass would have some set of non-static methods (and possibly fields) in its declaration.

This subclass could then be used as a type of a field, variable, type parameter or method argument (i.e. anywhere a type can be used). The compiler would have to keep track of the fact that this particular type could only be used in some restricted sense. (Imagine a method that returned a value of this subclass for example).

There are, I'm sure, many more gotcha's for this sort of thing.

So, bottom line, it would make writing a compiler really hard.

Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38