Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 977
user3421796 Avatar asked Apr 01 '14 23:04

user3421796


People also ask

How can a parent class constructor be called from a child class constructor?

You can call the base class constructor from the child class by using the super() which will execute the constructor of the base class.

Can we call a superclass constructor from the class constructor?

To explicitly call the superclass constructor from the subclass constructor, we use super() . It's a special form of the super keyword. super() can be used only inside the subclass constructor and must be the first statement.

Is it possible to call a subclass constructor from superclass constructor in Java?

A subclass can call a constructor defined by its superclass by use of the following form of super: super(parameter-list); Here, parameter-list specifies any parameters needed by the constructor in the superclass. super( ) must always be the first statement executed inside a subclass constructor.

Does a child constructor call the parent constructor Java?

If the child class constructor does not call super , the parent's constructor with no arguments will be implicitly called. If parent class implements a constructor with arguments and has no a constructor with no arguments, then the child constructors must explicitly call a parents constructor.


2 Answers

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.

like image 166
ajb Avatar answered Nov 20 '22 13:11

ajb


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

like image 43
peter.petrov Avatar answered Nov 20 '22 11:11

peter.petrov