Java中的重写[方法、用途、带输出的示例]
目录
重叠是指一个子类对父类中已经存在的方法进行实现。
从技术上讲,覆盖是一种要求子类或子类提供各种方法实现的功能,这些方法已经由它的一个超类或父类提供,在任何面向对象的编程语言 中。
当一个子类中的方法与它的超类中的方法具有相同的名称和签名时,该子类就起源于超类。
用于触发方法的对象指定了被执行的过程的变体。如果它用一个来自父类的对象来实现一个方法,将使用父类的版本。但如果用一个子类的对象来触发方法,将使用子类的版本。
例1:
代码:
// here we declare vehicle as a parent class
class Vehicle{
// here we create a method as vehicle has engine
// but changing the output as vehicle engine
void engine(){
System.out.println("this is vehicle engine");
}
}
// here we declare child class
// bike is based on vehicle category so its a child class
// we use extends keyword to call parent class
class Bike extends Vehicle{
// here we create method as same as in parent class
// but changing the output as bike engine
void engine(){
System.out.println ("this is bike engine");
}
}
// here we declare child class
// car is based on vehicle category so it's a child class
class Car extends Vehicle{
// here we create method as same as in parent class
// but changing the output as car engine
void engine(){
System.out.println ("this is car engine");
}
}
public class Code Example {
public static void main(String[] arg) {
// here we create object for bike
Bike honda = new Bike ();
honda.engine();// calling engine method
// here we create object for car
Car benz = new Car ();
benz.engine (); //calling engine method
}
}
输出:
this is bike engine
this is car engine
在上面的代码中,Vehicle类中的void engine()被称为重写方法。自行车类和汽车类中的void engine()方法被称为重写方法。
为什么重写在Java中是有用的?
如前所述,重写方法允许Java在运行时接受多态性。重载方法也是Java接受多态性 "一个应用,多种方法 "的另一种方式。
面向对象编程对代码重用和健壮性带来的最有效的影响是动态过程执行。使用现有的代码库来调用新类实例上的方法,而不需要重新编译,同时保留一个干净的抽象接口,这种能力是一个非常强大的武器。
重载方法允许人们从任何派生类对象中调用方法,而无需识别修改后的超类的形式。
什么时候在Java中应用重写是最理想的?
识别父类和子类形成一个从低级到高级的生产力的层次结构,是有效应用多态性 的秘密之一。
父类,如果使用得当,包含了子类直接访问的所有因素。它还定义了子类必须单独执行哪些方法。
这为子类提供了传达其方法的能力,同时保留了一个标准的接口。
一个父类将通过合并继承和重载的方法来决定其所有子类所使用的方法的一般形式。
例2:
代码:
//creating parent class
class Bank{
//create function to calculate interest
int getRateOfInterest(){return 0;}
}
//Creating child classes.
class SBI extends Bank{
//create function to calculate interest for SBI
int getRateOfInterest(){return 8;}
}
class ICICI extends Bank{
//create function to calculate interest for ICICI
int getRateOfInterest(){return 7;}
}
class AXIS extends Bank{
//create function to calculate interest for AXIS
int getRateOfInterest(){return 9;}
}
public class CodeExample {
public static void main(String[] arg) {
Bank sbibank=new SBI(); // create object for SBI
Bank icicibank=new ICICI(); // create object for ICICI
Bank axisbank=new AXIS(); // create object for AXIS
System.out.println("SBI Rate of Interest: "+sbibank.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+icicibank.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+axisbank.getRateOfInterest());
}
}
输出
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9
屏幕截图
Java中方法重写的规则是什么?
JAVA中的方法重写法则:
- 方法的名称应该是通用的,并且与父类中的名称相同。
- 方法中的方法签名(参数列表,返回类型)必须与父类中的相同。
- 类之间必须有继承关系。
- 父类中的所有抽象方法都应该在子类中被重写。
- 如果它将这些方法声明为静态或最终的,那么这些方法就不能被重写。
在重写中处理访问修改器
一个覆盖方法的活动增加会比被覆盖的方法有更多的访问权。父类中受保护的方法可以被公开,但在子类中不能被私有。如果你强行这样做,会引起编译时的错误。
在这种情况下,Java中的方法重写的目标是透明的。子类必须有它对这个过程的实现。
例3:
代码:
//Overriding and Access-Modifiers
//here we declare parent class
class Vehicle {
// private methods are not overridden
//if we create a function using the final keyword or static keyword word function can't be overridden in child class
private void engine()
{
System.out.println("Vehicle engine");
}
protected void fueltype()
{
System.out.println("Vehicle fueltype");
}
}
//here we declare child class
class Car extends Vehicle {
// unique to Child class
void engine()
{
System.out.println("Car engine");
}
protected void fueltype()
{
System.out.println("Car fueltype");
}
}
public class CodeExample {
public static void main(String[] arg) {
Vehicle vehicle = new Vehicle();
vehicle.fueltype();
Vehicle benz = new Car();
benz.fueltype();
// here we can't call engine type b'coz its private access modifier in parent class
// if we call getting error
}
}
输出:
From parent fuel type
From Car fuel type
屏幕截图
对于最终方法
如果你将任何方法声明为最终方法,就不能覆盖它。覆盖最终方法是不可能的。
对于构造器方法
很明显,构造函数会有它的类名,所以它也不能被覆盖。
对于抽象方法
如果抽象方法在接口或任何其他类中,子类应该覆盖。
对于静态方法
与最终方法类似,静态方法也不能被覆盖。父类中的静态方法将从子类中隐藏。
超类的实例方法 | 超类静态方法 | |
子类实例方法 | 覆盖 | 会产生一个编译时错误 |
子类静态方法 | 产生一个编译时的错误 | 隐藏 |
激活一个被覆盖的方法
super关键字将用于在覆盖方法中调用父类函数。
在JAVA中重写的错误处理
覆盖处理异常的方式有三种:
- 当父类没有声明异常时,子类可以只声明未检查的异常。
- 当父类声明了一个异常时,子类可以声明相同的异常,而不是任何其他的异常。
- 当父类已经声明了一个异常时,子类可以声明没有异常。
注意:
- 要在C++中实现覆盖或运行时多态性,你需要使用虚拟关键字。在Java中,方法在本质上是合成的。
- 多级过程覆盖是可行的。
- 通过覆盖,子类扩展了父类的能力。
- 方法覆盖同时实现了多态性和继承性,实现了代码的可扩展性。
结论
在Java中,当一个子类(子类)拥有与父类相同的方法时,就会发生方法覆盖。换句话说,当一个子类为其父类所声明的方法提供一个特定的实现时,方法覆盖就发生了。子类覆盖方法的能力允许一个类以 "足够接近 "的行动继承超类,然后根据需要改变它。覆盖方法的名称、数量和参数类型以及返回类型都与它所覆盖的方法相同。