在Java中使用 final 和继承
先决条件 - 在Java中覆盖,继承
final是Java中的一个关键字,用于限制某些功能。我们可以使用 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.0
height of s1 : 20.0
width of s2 : 10.0
height of s2 : 10.0
area of s1 : 200.0
area of s2 : 100.0
使用 final 防止继承
当一个类被声明为 final 时,它不能被子类化,即没有其他类可以扩展它。例如,在创建像预定义的 String 类这样的不可变类时,这特别有用。以下片段说明了带有类的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 时,它不能被子类覆盖。 Object 类就是这样做的——它的一些方法是最终的。以下片段说明了带有方法的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!");
}
}