-10

We learned in class that child classes don't inherit constructors and we should write our own. If we don't write one, java will provide a default one at run time.

Let's suppose there is a chain of parent and child classes inheriting down the line. Each has a constructor that has a simple println statement. Since the child is inheriting a parent, are those constructors going to be called and the statements print to the console even if there is no super call in child constructors?

The point I am trying to understand here is that even if you don't call the parent constructor, java will still go to the parent constructor and print any relevant info if that exists since the child extends the parent?

  • 4
    Why not try it out? – Turing85 May 01 '22 at 02:32
  • 3
    @Vishrant are you saying the post lacks effort? – Turing85 May 01 '22 at 02:37
  • This question is perfectly clear. It may be a duplicate (I didn't look), but it's not unclear. It certainly doesn't require immediate deletion. – Ryan M May 01 '22 at 03:26
  • 1
    @RyanM I share a different point of view on posting questions on SO without prior investigation, asking a clear question is one thing, but asking a clear question with specifics is another, and to me (and I could be wrong), this question lacks that effort, SO have an article on How to ask a good question. https://stackoverflow.com/help/how-to-ask, not sure why the 2 votes to close this question was retracted. – Vishrant May 01 '22 at 04:22
  • 5
    Lack of effort is not, and has never been, a reason to close a question, @Vishrant. It is, however, a fair reason to *downvote* a Q, because you think it shows inadequate research effort (see tooltip on the downvote btn). It's also a good indicator the question is a duplicate, but then, in order to close, you need to find the other question(s). There are only two ways that lack of effort could become a problem that must be addressed by closure: (A) not enough effort in *writing* the question, which makes it unclear, or (B) not enough effort to narrow down the problem, making it too broad. – Cody Gray - on strike May 01 '22 at 05:28
  • 1
    Does this answer your question? [JAVA inheritance constructor order of call](https://stackoverflow.com/questions/62112313/java-inheritance-constructor-order-of-call) – Turing85 May 01 '22 at 07:01

1 Answers1

-1

To quote this excellent Answer by cletus on a similar Question:

If the subclass constructor does not specify which superclass constructor to invoke then the compiler will automatically call the accessible no-args constructor in the superclass.

So let's try your scenario.

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Dog d = new Dog() ;
        System.out.println( d ) ;
    }
}

class Animal
{
    Animal() 
    {
        System.out.println( "Animal" ) ;
    }
}

class Mammal extends Animal
{
    Mammal() 
    {
        System.out.println( "Mammal" ) ;
    }
}

class Canine extends Mammal
{
    Canine() 
    {
        System.out.println( "Canine" ) ;
    }
}

class Dog extends Canine
{
    Dog() 
    {
        System.out.println( "Dog" ) ;
    }
}

See that code run live at IdeOne.com.

Animal
Mammal
Canine
Dog
Dog@452b3a41

Effectively, the compiler has added a call to the available no-arg constructor of each superclass, super(). Like this:

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Dog d = new Dog() ;
        System.out.println( d ) ;
    }
}

class Animal
{
    Animal() 
    {
        super() ;
        System.out.println( "Animal" ) ;
    }
}

class Mammal extends Animal
{
    Mammal() 
    {
        super() ;
        System.out.println( "Mammal" ) ;
    }
}

class Canine extends Mammal
{
    Canine() 
    {
        super() ;
        System.out.println( "Canine" ) ;
    }
}

class Dog extends Canine
{
    Dog() 
    {
        super() ;
        System.out.println( "Dog" ) ;
    }
}

You can write that call explicitly in your code, if you like, to make clear to the reader that you want to rely on the superclass’ available no-arg constructor being called. You can see the explicit calls working in this code on IdeOne.com.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154