我们先来看看这个名字的含义。 “超载”的意思是:给任何人原有的功能增加一些额外的负担,对吗?同时,“覆盖”意味着除了任何人原有的功能之外还提供新的功能。 在本文中,我们将详细了解 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.
}
}
输出:
#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.
}
}
输出:
超载注意事项
- 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
- The method must have the same name as in the parent class
- The method must have the same parameter as in the parent class.
- 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:
Limitations in method Overriding:
- The child class cannot override the private methods of the parent class.
- You cannot override final methods.
- 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中文网其他相关文章!