Java 演示由默认情况下的子类继承父类的默认构造函数的可用性
在这里,我们将通过Java程序演示默认情况下父类的默认构造函数对子类的可用性。
在深入讨论这个主题之前,让我们先熟悉一下术语 构造函数 , 父类 和 子类 。
构造函数
在Java中,构造函数是用于初始化对象的特殊方法。构造函数的名字与类名相同,且不返回任何值。
默认构造函数会在使用 new关键字 创建一个对象时自动调用。
Java中有以下三种类型的构造函数
- 默认构造函数。
-
带参数的构造函数。
-
拷贝构造函数。
在这里,我们只涉及默认构造函数。
默认构造函数没有参数,它会根据提供的数据类型默认初始化数据成员,比如0、0.0、null等。
父类和子类
作为派生类的基础的类被称为父类,而从基类派生或继承的类被称为子类。使用extend关键字进行继承。
示例
在以下的Java程序中,演示了Java中的继承机制以及父类和子类中的默认构造函数如何工作。它还展示了当创建一个子类的对象时,子类构造函数如何自动调用父类的默认构造函数。
// Java Program to demonstrate the Availability of Default
import java.io.*;
// creation of parent class named A
class A {
// default constructor of class A
A () {
System.out.println(
"This is a default constructor of class A");
}
}
// creation of a child class named B and inheriting parent class A to the
// child class B
class B extends A {
// default constructor of child class B
B () {
System.out.println(
"This is a default constructor of class B");
}
}
public class TutorialsPoint1 {
public static void main (String [] args) {
// creation of an object of parent class A that will
// only invoke the default constructor of the parent class
A o1 = new A ();
// creation of an object of child class B that will
// invoke a constructor of parent class A first and then
// the constructor of the child class
B o2 = new B ();
}
}
输出
This is a default constructor of class A
This is a default constructor of class A
This is a default constructor of class B
在上面的程序中,定义了一个父类A,它有一个默认构造函数,在被调用时只显示一条消息。
这里还定义了一个子类B,它继承自A,并且有自己的默认构造函数,在被调用时也会显示一条消息。
在程序的主方法中,创建了两个对象。第一个对象是通过A类的默认构造函数创建的,它只显示父类中定义的消息。第二个对象是通过B类的默认构造函数创建的,它先调用A类的构造函数显示其消息,然后再调用B类的构造函数显示其消息。
示例
以下Java程序演示了构造函数链的概念。它展示了当创建子类的对象时,子类构造函数会自动调用默认构造函数。它还展示了父类构造函数如何按照从上到下的层次结构方式被调用。
// Java Program to demonstrate the Availability of Default
// Constructor of the Super Class to the Sub Class by
// Default
import java.util.*;
class A {
// default constructor of the class named A
A() { System.out.println("\n This is a default constructor of class A "); }
}
class B extends A {
// default constructor of the class named B
B() { System.out.println("\n This is a default constructor of class B "); }
}
class C extends B {
// default constructor of the class named C
C() { System.out.println("\n This is a default constructor of class C "); }
}
public class TutorialsPoint2{
public static void main(String[] args){
// creation of an object of class C
// that will invoke the constructor of C
// but before invoking the constructor of class C
// the constructor of its parent
// class which is B will get invoked but C is a child of class A Thus,
// before invoking the constructor of class B, the constructor of the
// class A that is the parent of
// class B shall get invoked
C o = new C ();
}
}
输出
This is a default constructor of class A
This is a default constructor of class B
This is a default constructor of class C
在上面的程序中,定义了三个类,分别是A、B和C。类A有一个默认构造函数,在调用时简单地显示一条消息。类B继承了A,并有自己的默认构造函数,在调用时也显示一条消息。类C继承了B,并有一个默认构造函数,在调用时显示一条消息。
在程序的main方法中,创建了一个类C的对象。在调用类C的构造函数之后,首先调用了其直接父类B的构造函数来显示其消息,然后调用了类A的构造函数,类A是类B的父类,用来显示其消息。
最后,调用了类C本身的构造函数来显示其消息。
示例
下面的Java程序演示了在Java中使用super()关键字来调用父类的构造函数以及构造函数链在具有多级继承的类层次结构中的工作方式。super()关键字可以用来调用父类的默认构造函数或带参数的构造函数。
// Java Program to demonstrate the Availability of DefaultvConstructor of the Super Class to the Sub Class by Default
import java.util.*;
class A {
// default constructor of a class named A
A () { System.out.println("This is a default constructor of class A "); }
}
class B extends A {
// default constructor of a class named B
B () {
// java compiler invokes keyword super () by
// default in the case of the default constructor
super ();
System.out.println("This is a default constructor of class B ");
}
}
class C extends B {
// default constructor of a class named C
C (){
// java compiler invokes the keyword super() by
// default in the case of the default constructor
super ();
System.out.println("This is a default constructor of class C ");
}
}
public class TutorialsPoint3 {
public static void main(String[] args){
// creation of an object of class C
// that will invoke the constructor of C
// but before invoking the constructor of class C
// the constructor of its parent
// class shall be invoked which is B but B is a child of A class so,
// before invoking the constructor of the B class, it
// will invoke the constructor of A class that is the parent of
// class B
C obj = new C ();
}
}
输出
This is a default constructor of class A
This is a default constructor of class B
This is a default constructor of class C
在上面的程序中,定义了三个类,分别是 A 、 B 和 C 。
类A有一个默认的构造函数,当它被调用时只显示一条消息。类B继承自A,并有它自己的默认构造函数,该构造函数首先通过 super() 关键字调用其父类A的构造函数,然后显示自己的消息。类C继承自B,并有一个默认构造函数,该构造函数首先通过 super() 关键字调用其父类B的构造函数,然后显示自己的消息。
在程序的main方法中,创建了一个类C的对象。当类C的构造函数被调用时,它首先使用 super() 关键字调用其直接父类B的构造函数显示其消息,而类B的构造函数又通过 super() 关键字调用类A的构造函数显示其消息。
最后,调用类C自身的构造函数来显示其消息。
结论
本文介绍了一些方法来演示子类默认情况下如何访问超类的默认构造函数。
讨论从 构造函数 、 超类 和 子类 的概念开始。接着,讨论了Java中三种方法及其实现来执行所述操作。