ホームページ  >  記事  >  Java  >  Java でのオーバーロードとオーバーライド

Java でのオーバーロードとオーバーライド

WBOY
WBOYオリジナル
2024-08-30 15:43:55605ブラウズ

まず、名前が一見して何を示唆しているかを見てみましょう。 「過負荷」とは、誰かの本来の機能に余分な負担をかけることを意味しますよね?一方、「オーバーライド」とは、本来の機能に加えて新しい機能を提供することを意味します。  この記事では、Java のオーバーロードとオーバーライドについて詳しく説明します。はい、これらを Java でプログラム的に実装する場合も、同じアプローチに従います。一つずつ見ていきましょう。

Java でのオーバーロード

Java クラスに同じ名前で引数が異なる複数のメソッドがある場合、それをメソッドのオーバーロードと呼びます。名前を同じにすることで、プログラム コードの可読性が向上するだけです。たとえば、与えられた数値に対して加算演算を実行する必要があるとします。メソッドの名前を「addition()」としましょう。  ここで、加算は 2 つ、3 つ、またはそれ以上の間で行うことができます。したがって、追加の演算に含まれる数値の数に応じて、関数の引数 (またはパラメーター) を変更できます。しかし、これをせずに、引数の数ごとに異なるメソッドを書いてしまうと、名前が違って分かりにくくなってしまいます。  したがって、オーバーロードすることで、コードの可読性が向上します。さて問題は、どうやって過負荷を達成するかということです。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

コード例を使用して、これらを 1 つずつ見てみましょう。

#1 – パラメータの数を変更する

ここでは、いくつかの数値に対して追加の演算を実行します。このために、「AdditionOperation」というクラスを作成しましょう。そのクラス内に、「addition()」という名前の 2 つのメソッドを用意しましょう。それらの方法の 1 つでは、2 つの数値を加算します。もう 1 つは、3 つの数字を追加します。これらのメソッドのパラメータの数を変更することでこれを実現しますが、名前は同じにしておきます。このようにして、ここではメソッド「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 – データ型を変更する

ここでは、異なる型、たとえば integer 型と double 型の間で加算演算を実行します。このために、「AdditionOperation」というクラスを作成しましょう。そのクラス内に、「addition()」という名前の 2 つのメソッドを持たせます。これらのメソッドの 1 つでは、2 つの整数を加算します。もう 1 つは、ダブルを 2 つ追加します。これらのメソッドのパラメータのタイプを変更することでこれを実現しますが、名前は同じにしておきます。このようにして、ここではメソッド「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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。