user3421796
user3421796

Reputation: 107

In Java, can you call a parent class's superclass constructor through the constructor of the child class of the parent class?

I know the wording is a bit confusing and strange, but bear with me, I have no clue how to phrase that any shorter.

Say you have a class called SuperBlah, and you inherited it in a class called Blah then you inherited Blah into a class called ChildBlah (So SuperBlah-Blah-ChildBlah). Would the super(); keyword used in the ChildBlah's constructor called the SuperBlah's constructor if Blah doesn't have a constructor?

To those of you who said no, then why does this work? We have a class called BlusterBug that extends Critter class, and calls the super in the BlusterBug's constructor. Critter doesn't have a constructor, but the Class that Critter extends does have a constructor. (I purposefully omitted the rest of the code on the classes)

public class BlusterCritter extends Critter {
    // instance vaiables
    private int courageFactor;
    private static final double DARKENING_FACTOR = 0.05;
    // create a constructor(include what is necesary for parent)
    public BlusterCritter(int c)
    {
        super();
        courageFactor = c;
    }

Then in the Critter class, it doesn't have any constructors!

public class Critter extends Actor// omitted all the code
{
    /**
     * A critter acts by getting a list of other actors, processing that list,
     * getting locations to move to, selecting one of them, and moving to the
     * selected location.
     */
    public void act()
    {
        if (getGrid() == null)
            return;
        ArrayList<Actor> actors = getActors();
        processActors(actors);
        ArrayList<Location> moveLocs = getMoveLocations();
        Location loc = selectMoveLocation(moveLocs);
        makeMove(loc);
    }

But then the Actor class has a constructor!

public class Actor
{
    private Grid<Actor> grid;
    private Location location;
    private int direction;
    private Color color;

    /**
     * Constructs a blue actor that is facing north.
     */
    public Actor()
    {
        color = Color.BLUE;
        direction = Location.NORTH;
        grid = null;
        location = null;
    }

And the weirdest part? The program works perfectly fine and the compiler doesn't do catch any errors! How is this happening?

Upvotes: 7

Views: 587

Answers (5)

Bohemian
Bohemian

Reputation: 425208

No. Constructors aren't like methods in the respect of optional overriding by an intermediate class in the hierarchy. If an instance method of the super class is not implemented in the intermediate class, calling the method from the subclass via super.method() will invoke the superclass method. But a constructor for every class in the hierarchy must be called - you can't skip over the intermediate class's constructors.

Upvotes: 0

ajb
ajb

Reputation: 31699

If Blah doesn't have any constructor, the compiler will generate an implicit no-argument constructor for Blah that is just super()--that is, it will call SuperBlah's constructor.

So when ChildBlah's constructor calls super(), it will call this implicit constructor that the compiler generated for Blah, which results in SuperBlah's constructor being called. So in some sense, I think the answer to your question is "yes".

But this only works if Blah has no constructors. (And in your actual code, Critter doesn't have any constructors, so it works.) If any other constructors are defined for Blah, but there's no accessible no-argument constructor, then there won't be an implicit no-argument constructor, and the result will be an error at compile time.

Upvotes: 4

Robert Balent
Robert Balent

Reputation: 1462

Yes it will. If Blah doesn't have any constructor, it will call default empty constructor and transitively it will call non-parametric SuperBlah constructor.

Upvotes: 1

Anubian Noob
Anubian Noob

Reputation: 13596

It will call Blah's constructor, throwing a compiler error if Blah doesn't have one.

Upvotes: 0

peter.petrov
peter.petrov

Reputation: 39477

No, it won't. It will call the implicit default constructor in Blah.
If Blah defines other constructors (with parameters) but has no
default constructor, then this will generate a compilation error.

Also, note that constructors are not inherited
(if that relates in any way to your question/thoughts).

Upvotes: 2

Related Questions