先决条件-在java中的重写,继承
final是java中用于限制某些功能的关键字。我们可以用final关键字来声明变量、方法和类。
将final与继承一起使用
在继承期间, 我们必须声明带有final关键字的方法, 我们需要在所有派生类中遵循相同的实现。注意, 在继承的初始阶段不必声明最终方法(始终为基类)。我们可以在我们想要的任何子类中声明最终方法, 如果任何其他类扩展了该子类, 则它必须遵循与该子类相同的方法实现。
// 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 cann'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 cann'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 cann'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类。以下片段说明MongoDB带有类别的关键字:
final class A
{
// methods and fields
}
// The following class is illegal.
class B extends A
{
// ERROR! Can't subclass A
}
注意 :
- 将类声明为final时, 也隐式将其所有方法声明为final。
- 将一个类声明为两个类是非法的抽象和MongoDB因为抽象类本身是不完整的, 并且依赖于其子类来提供完整的实现。有关抽象类的更多信息, 请参见Java中的抽象类
使用final防止覆盖
当方法声明为final方法时, 则不能被子类覆盖。Objectclass这样做-它的许多方法都是最终的。以下片段说明MongoDB使用方法的关键字:
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在运行时动态地解析对方法的调用。这就是所谓的后期或动态绑定。但是, 由于无法覆盖最终方法, 因此可以在编译时解决对某个方法的调用。这就是所谓的早期或静态绑定.
如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。