Wednesday, 13 December 2017

Understanding of Constructors in Java

Before to this please read the constructors in java post. To Continue to this post  we have below thing need to know about the constructors in inheritance.

If we did not specify any constructors explicitly, no issues. Java will take care with Implicit constructors. But if we define explicit constructors we should know below points.

1. If we specify only one no arg default constructor in parent class explicitly, we no need to specify any explicit of constructors in sub class and if we create object of subclass, it will automatically calls the no arg explicit default constructor of parent class.

public class ConstructorTest{
    String mute1 = "java";
    //mute1 = new String("program");
   
    public ConstructorTest() {
        System.out.println("default parent const");
    }
    /*public ConstructorTest(String muteq) {
        System.out.println("default param parent const");
    }*/
    public static void main(String args[]){
        //ConstructorTest constructorTest= new ConstructorTest("rest");
        //System.out.println(constructorTest.mute1);
    }

}

public class DefaultConstructorTest extends constructorTest{
   
    /*public DefaultConstructorTest() {
        System.out.println("def const");
    }*/

    /*public DefaultConstructorTest(String muteq) {
        super(muteq);
        System.out.println("def param sub const");
    }*/

    public static void main(String[] args) {
        //DefaultConstructorTest constructorTest = new DefaultConstructorTest("test");
        DefaultConstructorTest constructorTest = new DefaultConstructorTest();
    }

}

output : default parent const

2. If we define one more parameterized constructor in parent class, then we no need to define any thing and it will work as above scenario.

public class ConstructorTest{
    String mute1 = "java";
    //mute1 = new String("program");
   
    public constructorTest() {
        System.out.println("default parent const");
    }
    public constructorTest(String muteq) {
        System.out.println("default param parent const");
    }
    public static void main(String args[]){
        //ConstructorTest constructorTest = new ConstructorTest("rest");
        //System.out.println(constructorTest.mute1);
    }

}


public class DefaultConstructorTest extends ConstructorTest{
   
    /*public DefaultConstructorTest() {
        System.out.println("def const");
    }*/

    /*public DefaultConstructorTest(String muteq) {
        super(muteq);
        System.out.println("def param sub const");
    }*/

    public static void main(String[] args) {
        //DefaultConstructorTest constructorTest = new DefaultConstructorTest("test");
        DefaultConstructorTest constructorTest = new DefaultConstructorTest();
    }

}
output : default parent const

3. if we specify only paramertized constructor in parent class, then we should define parameterized constructor in subclass and should call super class constructor as shwon below.


public class ConstructorTest{
    String mute1 = "java";
    //mute1 = new String("program");
   
    /*public ConstructorTest() {
        System.out.println("default parent const");
    }*/
    public ConstructorTest(String muteq) {
        System.out.println("default param parent const");
    }
    public static void main(String args[]){
        //ConstructorTest constructorTest= new ConstructorTest("rest");
        //System.out.println(constructorTest.mute1);
    }

}


/**
 * Implicit super constructor ConstructorTest() is undefined for default
 * constructor. Must define an explicit constructor
 *
 * @author
 *
 */
public class DefaultConstructorTest extends ConstructorTest{
   
    // will get error :Implicit super constructor ConstructorTest() is
    // undefined. Must explicitly invoke another constructor. so please comment it.
    /*public DefaultConstructorTest() {
        System.out.println("def const");
    }*/

    /**
     * We should invoke super class constructor explictly other wise it will
     * error Implicit super constructor ConstructorTest() is undefined. Must
     * explicitly invoke another constructor
     *
     * @param muteq
     */
    public DefaultConstructorTest(String muteq) {
        super(muteq);
        System.out.println("def param sub const");
    }

    public static void main(String[] args) {
        DefaultConstructorTest constructorTest = new DefaultConstructorTest("test");
        //DefaultConstructorTest constructorTest = new DefaultConstructorTest();
    }

}

4. If we have default constructor and paramterized constructor exlplicitly in parent class, then no need to define any explict constructors in sub class.


public class ConstructorTest {
    String mute1 = new String("java");
    //mute1 = new String("program");
    int j;
   
    public ConstructorTest() {
        System.out.println("default parent const");
    }
    public void modifySTring(){
        mute1 = "program";
    }
    public ConstructorTest(String muteq) {
        System.out.println("default param parent const");
    }
    public static void main(String args[]){
        ConstructorTest stringMutableTest = new ConstructorTest("rest");
        stringMutableTest.modifySTring();
        System.out.println(stringMutableTest.mute1);
    }

}
 


/**
 * Implicit super constructor StringMutableTest() is undefined for default
 * constructor. Must define an explicit constructor
 *
 * @author balakrishnab4
 *
 */
public class DefaultConstructorTest extends ConstructorTest{
   
    // will get error :Implicit super constructor StringMutableTest() is
    // undefined. Must explicitly invoke another constructor. so please comment it.
    /*public DefaultConstructorTest() {
        System.out.println("def const");
    }*/

    /**
     * We should invoke super class constructor explictly other wise it will
     * error Implicit super constructor StringMutableTest() is undefined. Must
     * explicitly invoke another constructor
     *
     * @param muteq
     */
    /*public DefaultConstructorTest(String muteq) {
    //    super(muteq);
        System.out.println("def param sub const");
    }*/

    public static void main(String[] args) {
        //DefaultConstructorTest constructorTest = new DefaultConstructorTest("test");
        DefaultConstructorTest constructorTest = new DefaultConstructorTest();
    }

}
 

Output: default parent const 

Note: We need to take care of child class, when we define only parameterized constructor in parent class.

No comments:

Post a Comment