首页  >  文章  >  Java  >  Java 中的最终类

Java 中的最终类

WBOY
WBOY原创
2024-08-30 16:01:14176浏览

带有 Final 一词的类声明称为最终类。 Java中的Final类不能被其他类继承,也不能被其他类扩展。最终类可以扩展其他类;它可以是子类,但不能是超类。创建不可变类时,最终类是最好的答案。最终类是一个声明,表明它是最终的,即该类没有子类并且不能进一步扩展。除了不变性之外,最后一类还保留了一些更重要的内容。

有时,出于安全目的,最佳实践是使用最终类,因为某些类执行与安全相关的操作,例如身份验证和授权。在这种情况下,类内部的字符串和方法在应用程序的整个生命周期中都不应该被修改,那么将类设为 Final 是更好的做法。

广告 该类别中的热门课程 财务建模和估值 - 专业化 | 51 课程系列 | 30 次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

制作类,Final提高了应用程序的效率。

语法:

final class Student {
private int age = 3;
}
/* // Trying to inherit the Student class
class Info extends Student{
public static void main(String[] args) {
//creating instance of the HeavyVehicle class
System.out.println(“I am unable to inherit the class with the final keyword”);
}
}

以上给出的课程具有最终决定权。用另一个类扩展这个最终类将产生诸如“错误:无法从最终学生继承”之类的错误,如下面的屏幕截图所示。

Java 中的最终类

Final Class 在 Java 中是如何工作的?

当使用final关键字声明一个类时,它使JVM能够做出假设和优化。一旦将final关键字添加到Java中的任何类中,该类的引用就无法更改。编译器将检查是否重新发生任何引用,从而产生错误。

如果我们将任何类声明为最终类并实例化任何最终类,会发生什么?然后在池区中创建;在池中创建的对象具有特殊的不变性功能。不可变 Java 类的一些最佳示例是 String、Integer、Double 等。

如果将一个类声明为final,那么它会隐式将该类的所有方法声明为final。 Final是Java中的修饰符;您可以在变量、方法或类上使用final关键字。一旦将修饰符添加到类中,该类的所有变量和方法都将隐式不可变。使类不可变有几个好处,例如它是只读的,并且无法在没有任何同步的情况下在多个线程中安全地共享。

Java 中的 Final Class 示例

给出了以下 Java 中 Final 类的示例:

示例#1

HeavyVehicle 类不允许在给定程序中继承,因为它被声明为最终类。

代码:

//This class will not be extended
final class HeavyVehicle{
void messages(){
System.out.println("Your Vehicle Insurance is going to be expire in the next month");
}
}
// Inheriting HeavyVehicle class is not allowed as it is a final class
/*class Vehicle extends HeavyVehicle{
}
*/
//main class
class Car{
public static void main(String[] args) {
//creating instance of the HeavyVehicle class
HeavyVehicle hvObj = new HeavyVehicle();
hvObj.messages();
}
}

如果我们尝试继承 HeavyVehicle 类,我们将收到以下错误,如下面的屏幕截图所示。

Java 中的最终类

现在我们将转到下一个类,在其中创建最终类的实例以使用其方法。下面是跳过继承过程后程序的输出。

Java 中的最终类

示例#2

在这个程序中,HeavyVehicle类被声明为final类,因此它不能在其他类中扩展,因此它显式地将其引用传递给Vehicle类方法。

代码:

//This class will not be extended
final class HeavyVehicle{
public double getDistanceCanCover(int mileage, double fuelStatus) {
return mileage * fuelStatus;
}
}
class Vehicle{
private int mileage;
private double fuelStatus;
Vehicle(int mileage, double fuelStatus){
this.mileage = mileage;
this.fuelStatus = fuelStatus;
}
public void DisplayDistanceCanCover(HeavyVehicle hvObj) {
double distCanCover = hvObj.getDistanceCanCover(this.mileage, this.fuelStatus);
System.out.println("Distance Vehicle can cover in the available fuel: " + distCanCover);
}
}
//main class
class Car {
public static void main(String[] args) {
//creating instance of the HeavyVehicle class
HeavyVehicle hvObj = new HeavyVehicle();
//creating instance of the Vehicle class
Vehicle vehObj = new Vehicle(30, 3.5);
//calling the method of the
vehObj.DisplayDistanceCanCover(hvObj);
}
}

在上面给出的示例中,我们可以看到如何有效地使用 Final 类。上述程序的输出如下。

输出

Java 中的最终类

示例 #3

在此示例中,最终类继承了另一个类,即 Student,但没有其他类可以扩展 Info 类,因为它是最终类。

代码:

class Student{
public void showDetails(String name, String email, int age) {
System.out.println("\nStudent Name : "+ name);
System.out.println("\nStudent Email : "+ email);
System.out.println("\nStudent Age : "+ age);
}
}
// Trying to inherit the Student class
final class Info extends Student{
//main method of java to start program execution
public static void main(String[] args) {
//creating instance of this class
Info infoObj  = new Info();
//calling here method of the parent class
infoObj.showDetails("Alex Carry", "[email protected]", 40);
}
}

输出:

Java 中的最终类

结论

在上面的文章中,我们了解了 Java 中 Final 类的重要性。我还回顾了人们通常将 Java 中的 Final 类称为不可变类。我注意到与宣布期末课程相关的一些要点。在给定的示例中,我们可以看到如何通过引用传递最终类,从而在其他类中有效地使用它。

以上是Java 中的最终类的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Java File Class下一篇:Stack Class in Java