简体   繁体   中英

Java: Inheriting constructor to the subclass

As i knew, constructors,Instance Initialization block are NOT inherited to the subclass, but below code inherits the super-class constructor, why it is calling?

Expected output is : from cons 2

but its showing output like : --IIB-- from cons 1 from cons 2

WHY? this output , *As i know "sub class should not inherit Constructor & IIB block"* 

Please help me to make clear this concept.

public class X
{ 
    {
        System.out.println("--IIB--");
    }       

    X()
    {
        System.out.println("from cons 1");
    }
}   

class Y extends X
{
     Y()
     {
         System.out.print("from cons 2");
     }
}    


class Z 
{
    public static void main(String args[])
    {
        Y ob=new Y(); 
    }
} 

This is happening because this:

Y()
 {
     System.out.print("from cons 2");
 }

actually becomes

Y()
 {
     super();  //Call to X's constructor made here even if you don't call it
     System.out.print("from cons 2");
 }

The reason for this is that every Y is also an instance of X . It's necessary to call that parent constructor first, before any of the child constructor executes, to guaranteee that the parent attributes are ready to go.

Edit : here's the example to show that "super class constructors are not inheriated by the subclass":

class A {
    A(int intForA) { 
        //Do stuff...
    }
}

class B extends A {
    B() {
        this(3);  //Won't compile! A's constructor isn't inherited by B
    }
}

Instead, we do this:

class B extends A {
    B() {
        super(3);  //This compiles; we're calling A's constructor from B
    }
}

"The Java compiler copies initializer blocks into every constructor." - http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

"If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass." - http://docs.oracle.com/javase/tutorial/java/IandI/super.html

When you call the sub class constructor, it internally calls the super class constructor using the super() (Note section in the linked page at the last) and in your case, the super class for Y is X . Also, the instance blocks are copied in the constructor and thus are executed when any of the constructors of that class are called.

From the init block docs

Initializer blocks for instance variables look just like static initializer blocks, but without the static keyword:

{
    // whatever code is needed for initialization goes here
}

The Java compiler copies initializer blocks into every constructor.

Thus the output in the below order.

--IIB-- - From the instance block which gets placed inside the constructor of X .

from cons 1 - when super() is called inside Y()

from cons 2 - the SOP in Y()

in you code Y extends X so ideally when you create Y class object it also create X class object. So its block gets execute first then constructor of X and then constructor of Y .

Hence you are getting output like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM