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,並且有一個預設建構函式,在被呼叫時顯示一條訊息。
在程式的主方法中,建立了一個C類的物件。在呼叫C類的建構函式後,它首先呼叫其直接父類B的建構函式來顯示其訊息,然後呼叫B的父類A的建構函式來顯示其訊息。
最後,呼叫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的建構函式,然後顯示它自己的訊息。
在程式的主方法中,建立了一個C類的物件。當呼叫C類的建構函式時,它首先使用`super()`關鍵字呼叫其直接父類B的建構函式來顯示其訊息,然後B類再使用`super()`關鍵字呼叫A類的建構函式來顯示其訊息。
最後,呼叫C類的建構函式本身來顯示其訊息。
結論
本文闡述了演示父類預設建構函式對子類預設可用性的方法。
討論從**建構函式**、**父類**和**子類**的概念開始。此外,還討論了在Java中執行上述操作的三種方法及其實現。