java final与继承_在Java中使用final进行继承

Java 面向对象/Java 继承

在Java中使用final进行继承

final是用于限制某些功能的java中的一个关键字。我们可以用final关键字声明变量,方法和类。

使用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 finalso any class extending

// Rectangle cann't override it

@Override

final double getArea()

{

return this.getHeight() * this.getWidth();

}

}

//derived class two

class Square extends Shape

{

// Rectangle 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类这样的不可变类时。以下片段用类说明final关键字:

final class A

{

// methods and fields

}

// The following class is illegal.

class B extends A

{

// ERROR! Can't subclass A

}

注意 :

声明一个类最终隐含地声明它的所有方法也是最终的。

它是非法的声明一个类既是抽象的,并最终因为一个抽象类是本身不完整的,在它的子类依靠提供完整的实现。

使用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!");

}

}

通常,Java在运行时动态地解析对方法的调用。这被称为晚期或动态绑定。但是,由于最终方法不能被覆盖,所以可以在编译时解决对方法的调用。这被称为早期或静态绑定。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值