首頁 > Java > java教程 > Java 中的重載與覆蓋

Java 中的重載與覆蓋

WBOY
發布: 2024-08-30 15:43:55
原創
695 人瀏覽過

我們先來看看這個名字的意思。 「超載」的意思是:給任何人原有的功能增加一些額外的負擔,對吧?同時,「覆蓋」意味著除了任何人原有的功能之外還提供新的功能。  在本文中,我們將詳細了解 Java 中的重載和覆蓋範圍。是的,在 Java 中以程式設計方式實作這些遵循相同的方法。讓我們一一來看看。

Java 中的重載

當一個java類別有多個同名但參數不同的方法時,我們稱之為方法重載。透過保持名稱相同,我們只是增加了程式碼的可讀性。例如,假設我們需要對某些給定的數字執行加法運算。假設我們的方法的名稱是「addition()」。  這裡,可以在兩個、三個或更多之間進行加法。因此,根據附加操作將涉及多少個數字,我們可以更改函數的參數(或多個參數)。但是,如果您為不同數量的參數編寫不同的方法,則將很難識別,因為名稱會不同。  因此,透過重載,我們可以提高程式碼的可讀性。那麼現在的問題是,我們要如何實現重載?

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

讓我們透過範例程式碼逐一查看這些內容。

#1 – 透過修改參數數量

所以在這裡,我們將對一些數字進行額外的操作。為此,讓我們建立一個名為「AdditionOperation」的類別。在該類別中,我們有兩個名為「addition()」的方法。在其中一種方法中,我們將添加兩個數字。在另一個中,我們將添加三個數字。我們將透過更改這些方法中的參數數量來實現此目的,但我們將保留相同的名稱。這樣,我們在這裡重載了「addition()」方法。

代碼:

public class AdditionOperation {
static int addition(int num1,int num2){return num1+num2;} //function declarationand definition for addition of two numbers
static int addition(int num1,int num2,int num3){return num1+num2+num3;} //function declarationand definition for addition of three numbers
public static void main(String args[]) {
system.out.printin(addition(35,36)); //method overloading
system.out.printin(addition(35,36,37)); //method overloading, we are calling same methods but for different number of arguments.
}
}
登入後複製

輸出:

Java 中的重載與覆蓋

#2 – 透過修改資料型別

在這裡,我們將對不同類型進行加法運算,例如整數和雙精度類型之間。為此,讓我們建立一個名為「AdditionOperation」的類別。在該類別中,我們有兩個名為「addition()」的方法。在其中一種方法中,我們將兩個整數相加。在另一個中,我們將添加兩個雙打。我們將透過更改這些方法中的參數類型來實現此目的,但我們將保留相同的名稱。這樣,我們在這裡重載了「addition()」方法。

代碼:

public class additionOperation {
static int addition(int num1,int num2){return num1+num2;} //function declarationand definition for addition of two numbers
static double addition(double num1,num2){return num1+num2;} //function declarationand definition for addition of three numbers
public static void main(String args[]) {
system.out.printin(addition(35,36)); //method overloading
system.out.printin(addition(35.5,36.6)); //method overloading, we are calling same methods but for different type of arguments.
}
}
登入後複製

輸出:

Java 中的重載與覆蓋

超載注意事項

  • Overloading in java is basically a “compile-time polym Method Overloading in C#orphism”. Compile-time polymorphism in java is also called “Static method Dispatch” or “Early binding”. So what do I mean by that jargon?
  • As the name suggests, polymorphism is basically an ability to take many forms (poly: many, morph: form). The linking or binding of the overridden function and the object occurs at compile time. Compile-time polymorphism earns its name because function binding to an object occurs early, specifically during compile time, rather than at runtime. It is also called “Early binding” to emphasize this early binding process.
  • Static dispatch is a type of polymorphism or method dispatch that tells how java will select which functionality of the method will be used in compile time. (I mean, whether it will add two or three numbers in our coding example).  The name is also known as the Static method Dispatch.

Overriding in Java

  • When a java subclass or child class has a method that is of the same name and contains the same parameters or arguments and a similar return type as a method that is present in its superclass or parent class, then we can call the method of the child class as an overridden method of the method of its parent class.
  • For example, suppose we need to perform some display operation according to its class type. If I call the method of a parent class, it will display a message defined in a parent class. But, calling the child class method will override the display message of its parent class and display the message defined within the child class method. Hence depending on which display we need to show, we can call the related class (parent or child). We are not changing the method name, argument, and return type here. We are just changing the functionality of the method in the child class. But, instead of this, if we do not do overriding, i. e. we don’t give the specific implementation of the child method, then while calling the method, it will display the same message as present in a parent class.
  • When writing code, use the @Override annotation before overriding a method. This annotation informs the compiler that the method must override a declaration in a superclass. Although using this is not mandatory, it helps prevent errors. If a method annotated with @Override fails to override a method, the compiler generates an error.

Rules for Java Method Overriding

  1. The method must have the same name as in the parent class
  2. The method must have the same parameter as in the parent class.
  3. There must be an IS-A relationship (inheritance).

Code:

//Parent or Super class
class Parent {
public void display() {
system.out.printin("Hello, I am from parent class");
}
}
//Child or sub class
class Sub extends Parent {
//Below method overrides the Parent display() method
// @override
public void display() {
system.out.printin("Hello, I am from child class");
}
}
//Driver class
public class Overriding {
public static void main?(String args[])
{
Parent superObject = new Parent ();
superObject.display(); // Super class method is called
Parent subObject = new Sub();
subObject.display(); //Child class method is called by a parent type reference: this is functionality of method overriding
Sub subObject2 = new Sub(); //Child class method is called by a child type reference
subObject2.display();
}
}
登入後複製

Output:

Java 中的重載與覆蓋

Limitations in method Overriding:

  1. The child class cannot override the private methods of the parent class.
  2. You cannot override final methods.
  3. One cannot override static methods.

Points to be Noted for Overriding

  • Overriding in Java represents “run-time polymorphism,” also known as “dynamic method dispatch” or “late binding.” These terms refer to the ability of the Java runtime system to determine the appropriate method implementation to execute based on the actual type of the object at runtime.
  • As the name suggests, polymorphism is basically an ability to take many forms (poly: many, morph: form). So, a call to an overridden function with the object is done during the run time. Hence called run time polymorphism.
  • Late binding, or “dynamic binding,” occurs after compilation during runtime, as it binds functions to objects.
  • Dynamic dispatch is a type of polymorphism or method dispatch that tells how java will select which method functionality will be used in run time. The name is also known as Dynamic method Dispatch.

Conclusion

This concludes our learning of the topic “Overloading and Overriding in Java”. Write the codes mentioned above in the java compiler and check the output. Learning codes will be incomplete if you do not do hands-on by yourself, enhancing your coding skills.  Happy coding!!

以上是Java 中的重載與覆蓋的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板