📜  显示继承构造函数默认调用父构造函数的Java程序

📅  最后修改于: 2022-05-13 01:54:36.364000             🧑  作者: Mango

显示继承构造函数默认调用父构造函数的Java程序

在Java中,存在()在Java中称为超级一个非常重要的关键字的关键字,其被广泛用于在Java被并因此的传承进场面向对象的。因此,每当我们在子构造函数中使用 super 关键字时,它就会自行调用默认的父构造函数。

示例 1

Java
// Java Program to Demonstrate Inherited constructor
// calls the parent constructor by default
 
// Class 1
// Main class
class GFG {
    public static void main(String[] a)
    {
        // Inherited constructor calling
        new child();
        new parent();
    }
}
 
// Class 2
// Parent class - Helper class
class parent {
 
    // Constructor of parent class
    parent()
    {
        System.out.println("I am parent class constructor");
    }
}
 
// Class 3
// Child class - Helper class
class child extends parent {
 
    // Constructor of child class
    child()
    {
        System.out.println("I am child class constructor");
    }
}


Java
// Java Program to Demonstrate Inherited constructor
// calls the parent constructor by default
 
// Class 1
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] a)
    {
        // Inherited constructor calling
        new child();
        new child(100);
    }
}
 
// Class 2
// Parent class (Helper class)
class parent {
 
    // Constructor of parent class
    parent()
    {
        // Print statement
        System.out.println("I am parent class constructor");
    }
}
 
// Class 3
// Child class (Helper class)
class child extends parent {
 
    // Constructor 1
    // Constructor of child class
    child()
    {
        // Print statement
        System.out.println("I am child class constructor");
    }
 
    // Constructor 2
    // Constructor of child class
    child(int x)
    {
        // Print statement
        System.out.println(
            "I am param child class constructor");
    }
}



输出
I am parent class constructor
I am child class constructor
I am parent class constructor



示例 2

Java

// Java Program to Demonstrate Inherited constructor
// calls the parent constructor by default
 
// Class 1
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] a)
    {
        // Inherited constructor calling
        new child();
        new child(100);
    }
}
 
// Class 2
// Parent class (Helper class)
class parent {
 
    // Constructor of parent class
    parent()
    {
        // Print statement
        System.out.println("I am parent class constructor");
    }
}
 
// Class 3
// Child class (Helper class)
class child extends parent {
 
    // Constructor 1
    // Constructor of child class
    child()
    {
        // Print statement
        System.out.println("I am child class constructor");
    }
 
    // Constructor 2
    // Constructor of child class
    child(int x)
    {
        // Print statement
        System.out.println(
            "I am param child class constructor");
    }
}


输出
I am parent class constructor
I am child class constructor
I am parent class constructor
I am param child class constructor