Java程序说明默认超类的默认构造函数到子类的可用性
Java中的构造函数是一种用于初始化对象的特殊方法。每当使用 new() 关键字创建对象时,至少会调用一个构造。构造函数名称必须与类名称匹配,并且不能有返回类型。如果在这种情况下类中没有可用的构造函数,则Java编译器默认提供默认构造函数(无参数构造函数) 。
默认构造函数(无参数构造函数)的目的是什么
默认构造函数用于根据类型为对象提供默认值,如 0、null 等。
默认构造函数示例
在下面给出的代码中,我们创建了一个名为 default Constructor 的类,在这个类中,我们创建了一个默认构造函数。
Java
// Java program to illustrate default constructor
import java.io.*;
class defaultConstructor {
int a;
double d;
String s;
// creating default constructor
defaultConstructor()
{
System.out.println("Hi I am a default constructor");
}
}
class GFG {
public static void main(String[] args)
{
// creating an object of clsss defaultConstructor
// after creating an object it will invoke the
// default constructor
defaultConstructor obj = new defaultConstructor();
// defalult constructor provide default values to
// the object
System.out.println(obj.a);
System.out.println(obj.d);
System.out.println(obj.s);
}
}
Java
// Java Program to Illustrate the Availability of Default
// Constructor of the Super Class to the Sub Class by
// Default
import java.io.*;
// creating parent class
class parent {
// default constructor of parent class
parent()
{
System.out.println(
"I am default constructor from parent class");
}
}
// creating child class and inheriting parent class to the
// child class
class child extends parent {
// default constructor of child class
child()
{
System.out.println(
"I am default constructor from child class");
}
}
class GFG {
public static void main(String[] args)
{
// creating object of parent class and this will
// only invoke parent class default constructor
parent obj1 = new parent();
// creating object of child class and this will
// invoke parent class constructor first and then it
// will invoke child class constructor
child obj2 = new child();
}
}
Java
// Java Program to Illustrate the Availability of Default
// Constructor of the Super Class to the Sub Class by
// Default
import java.util.*;
class z {
// default constructor of class z
z() { System.out.println("Hi I am z"); }
}
class y extends z {
// default constructor of class y
y() { System.out.println("Hi I am y"); }
}
class x extends y {
// default constructor of class x
x() { System.out.println("Hi I am x"); }
}
class GFG {
public static void main(String[] args)
{
// creating an object of class x
// this will invoke the constructor of x
// but before invoking the constructor of class x
// it will invoke the constructor of it's parent
// class which is y but y is child of z class so,
// before invoking the constructor of y class it
// will invoke the constructor of z class(parent of
// y class)
x obj = new x();
}
}
Java
// Java Program to Illustrate the Availability of Default
// Constructor of the Super Class to the Sub Class by
// Default
import java.util.*;
class z {
// default constructor of class z
z() { System.out.println("Hi I am z"); }
}
class y extends z {
// default constructor of class y
y()
{
// keyword super() is called by java compiler by
// default in case of default constructor
super();
System.out.println("Hi I am y");
}
}
class x extends y {
// default constructor of class x
x()
{
// keyword super() is called by java compiler by
// default in case of default constructor
super();
System.out.println("Hi I am x");
}
}
class GFG {
public static void main(String[] args)
{
// creating an object of class x
// this will invoke the constructor of x
// but before invoking the constructor of class x
// it will invoke the constructor of it's parent
// class which is y but y is child of z class so,
// before invoking the constructor of y class it
// will invoke the constructor of z class(parent of
// y class)
x obj = new x();
}
}
输出
Hi I am a default constructor
0
0.0
null
默认情况下超类的默认构造函数到子类的可用性。
当我们从父类继承到子类时,必须首先在子类构造函数中调用关键字 super()。如果在子类构造函数中没有调用 super() ,那么Java编译器将为我们执行此操作。这就是为什么每当我们创建子类的对象时也会调用父类构造函数的原因。
示例 1:
Java
// Java Program to Illustrate the Availability of Default
// Constructor of the Super Class to the Sub Class by
// Default
import java.io.*;
// creating parent class
class parent {
// default constructor of parent class
parent()
{
System.out.println(
"I am default constructor from parent class");
}
}
// creating child class and inheriting parent class to the
// child class
class child extends parent {
// default constructor of child class
child()
{
System.out.println(
"I am default constructor from child class");
}
}
class GFG {
public static void main(String[] args)
{
// creating object of parent class and this will
// only invoke parent class default constructor
parent obj1 = new parent();
// creating object of child class and this will
// invoke parent class constructor first and then it
// will invoke child class constructor
child obj2 = new child();
}
}
输出
I am default constructor from parent class
I am default constructor from parent class
I am default constructor from child class
示例 2:
Java
// Java Program to Illustrate the Availability of Default
// Constructor of the Super Class to the Sub Class by
// Default
import java.util.*;
class z {
// default constructor of class z
z() { System.out.println("Hi I am z"); }
}
class y extends z {
// default constructor of class y
y() { System.out.println("Hi I am y"); }
}
class x extends y {
// default constructor of class x
x() { System.out.println("Hi I am x"); }
}
class GFG {
public static void main(String[] args)
{
// creating an object of class x
// this will invoke the constructor of x
// but before invoking the constructor of class x
// it will invoke the constructor of it's parent
// class which is y but y is child of z class so,
// before invoking the constructor of y class it
// will invoke the constructor of z class(parent of
// y class)
x obj = new x();
}
}
输出
Hi I am z
Hi I am y
Hi I am x
示例 3:
在这里,我们只想展示关键字 super() 的使用,它是如何工作的
Java
// Java Program to Illustrate the Availability of Default
// Constructor of the Super Class to the Sub Class by
// Default
import java.util.*;
class z {
// default constructor of class z
z() { System.out.println("Hi I am z"); }
}
class y extends z {
// default constructor of class y
y()
{
// keyword super() is called by java compiler by
// default in case of default constructor
super();
System.out.println("Hi I am y");
}
}
class x extends y {
// default constructor of class x
x()
{
// keyword super() is called by java compiler by
// default in case of default constructor
super();
System.out.println("Hi I am x");
}
}
class GFG {
public static void main(String[] args)
{
// creating an object of class x
// this will invoke the constructor of x
// but before invoking the constructor of class x
// it will invoke the constructor of it's parent
// class which is y but y is child of z class so,
// before invoking the constructor of y class it
// will invoke the constructor of z class(parent of
// y class)
x obj = new x();
}
}
输出
Hi I am z
Hi I am y
Hi I am x