1

I have an abstract class MousableActor that extends a concrete class Actor:

public abstract class MousableActor extends Actor
{   
    /**
     * Constructs a MousableActor.
     */
    protected void MousableActor()
    {
    }
}

When I look at the javadoc generated for the class, I see a public no-args constructor: described in text

According to Section 8.8.9 of the JLS:

If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.

I always considered that an if-and-only-if. Why is a public default constructor being created even though I explicitly declared a protected constructor? Does it have something to do with the superclass having a public no-args constructor?

I am using Greenfoot version 2.4.2 (which shouldn't matter) on top of Java 1.8.0.

Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101

2 Answers2

5

A constructor is not a void method.

protected void MousableActor()

should be

protected MousableActor()
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • D'oh! I hadn't realized that a method could have the same name as a constructor, not that I was conscious of having typed "void". – Ellen Spertus Nov 21 '15 at 01:21
2

Because it's returning void, it's not a constructor; it's a method, so there are no constructors, and the default constructor is created.

If you intend for that to be a constructor, then remove void.

rgettman
  • 176,041
  • 30
  • 275
  • 357