先决条件—— java中的重写 , 遗产 最终的 是java中用于限制某些功能的关键字。我们可以用final关键字声明变量、方法和类。
使用final和继承
在继承过程中,我们必须用final关键字声明方法,我们需要在所有派生类中遵循相同的实现。注意,没有必要在继承的初始阶段声明final方法(基类总是)。我们可以在任何子类中声明final方法,如果任何其他类扩展了这个子类,那么它必须遵循与该子类中相同的方法实现。
JAVA
// Java program to illustrate // use of final with inheritance // base class abstract class Shape { private double width; private double height; // Shape class parameterized constructor public Shape( double width, double height) { this .width = width; this .height = height; } // getWidth method is declared as final // so any class extending // Shape can't override it public final double getWidth() { return width; } // getHeight method is declared as final // so any class extending Shape // can not override it public final double getHeight() { return height; } // method getArea() declared abstract because // it upon its subclasses to provide // complete implementation abstract double getArea(); } // derived class one class Rectangle extends Shape { // Rectangle class parameterized constructor public Rectangle( double width, double height) { // calling Shape class constructor super (width, height); } // getArea method is overridden and declared // as final so any class extending // Rectangle can't override it @Override final double getArea() { return this .getHeight() * this .getWidth(); } } //derived class two class Square extends Shape { // Square class parameterized constructor public Square( double side) { // calling Shape class constructor super (side, side); } // getArea method is overridden and declared as // final so any class extending // Square can't override it @Override final double getArea() { return this .getHeight() * this .getWidth(); } } // Driver class public class Test { public static void main(String[] args) { // creating Rectangle object Shape s1 = new Rectangle( 10 , 20 ); // creating Square object Shape s2 = new Square( 10 ); // getting width and height of s1 System.out.println( "width of s1 : " + s1.getWidth()); System.out.println( "height of s1 : " + s1.getHeight()); // getting width and height of s2 System.out.println( "width of s2 : " + s2.getWidth()); System.out.println( "height of s2 : " + s2.getHeight()); //getting area of s1 System.out.println( "area of s1 : " + s1.getArea()); //getting area of s2 System.out.println( "area of s2 : " + s2.getArea()); } } |
输出:
width of s1 : 10.0height of s1 : 20.0width of s2 : 10.0height of s2 : 10.0area of s1 : 200.0area of s2 : 100.0
使用final防止继承
当一个类被声明为final时,它就不能被子类化,也就是说,没有任何其他类可以扩展它。例如,当 创建不可变类 就像预定义的 一串 班下面的片段说明了 最终的 类的关键字:
final class A{ // methods and fields}// The following class is illegal.class B extends A { // ERROR! Can't subclass A}
注:
- 将类声明为final也会隐式地将其所有方法声明为final。
- 将一个类同时声明为 摘要 和 最终的 因为抽象类本身是不完整的,依赖于它的子类来提供完整的实现。有关抽象类的更多信息,请参阅 java中的抽象类
使用final防止重写
当一个方法被声明为final时,它不能被子类重写。这个 对象 类实现了这一点——它的许多方法都是最终的。下面的片段说明了 最终的 关键字和方法:
class A { final void m1() { System.out.println("This is a final method."); }}class B extends A { void m1() { // ERROR! Can't override. System.out.println("Illegal!"); }}
通常,Java会在运行时动态解析对方法的调用。这叫做 延迟绑定或动态绑定 但是,由于不能重写final方法,因此可以在编译时解析对final方法的调用。这叫做 早期或静态绑定 . 本文由 高拉夫·米格拉尼 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。