简体   繁体   English

无法子类化最终类的主题接口

[英]cannot subclass the final class Subject Interface

import javax.security.auth.Subject;

abstract class Department extends Subject{
    String name;


}

I have this piece of code. 我有这段代码。 Sadly this throws the following error: The type Department cannot subclass the final class Subject 遗憾的是,这将引发以下错误: The type Department cannot subclass the final class Subject

I was reading the documentation: https://docs.oracle.com/javase/7/docs/api/javax/security/auth/Subject.html but there is nothing much on how to use it(a specific use case). 我正在阅读文档: https : //docs.oracle.com/javase/7/docs/api/javax/security/auth/Subject.html,但是关于如何使用它(具体用例)没有太多内容。

Does anyone know how you can use Subject on an abstract Class (it has to be extended by an abstract class) ? 有谁知道如何在抽象类上使用Subject(必须由抽象类扩展)?

Edit: 编辑: 在此处输入图片说明

Edit 2: Error Using Implements: 编辑2:使用工具时出错:

The type Subject cannot be a superinterface of Department; a superinterface must be an interface

Since Subject class is a final class you won't be able to extend it. 由于Subject classfinal class您将无法扩展它。 That's the purpose of the final keyword. 这就是final关键字的目的。 The only way I see is wrapping the class and its methods. 我看到的唯一方法是包装类及其方法。 You won't be able to use the wrapper where the final class is required, but you can implement any interfaces which are on the final class. 您将无法在需要最终类的地方使用包装器,但是可以实现最终类上的任何接口。

ex: 例如:

final class Foo {
    public String getSomething() {
        //
    }
}

class Bar {
    Foo foo;

    public Bar() {
        foo = new Foo();
    }

    public String getSomething() {
        foo.getSomething();
    }

    public String doSomethingElse() {
        //
    }
}

Not sure it would be helpful in your scenario tho. 不确定是否会对您的情况有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM