简体   繁体   中英

java - extend a class to a library superclass

I need to extend my own class to a library class ( org.jsoup.nodes.Node ). The superclass is abstract and contains abstract methods. My problem is that some of these abstract methods have no modifier, which mean that by default I have no access and I can't override them. When I try to do it I have this message:

Method does not override method from its superclass

How could I solve this problem?

A method with no access modifier is package-private. The only way to subclass the class you reference is to declare your class in the package org.jsoup.nodes , so it's in the same package.

package org.jsoup.nodes;

class MyNode extends Node {
    // ...
}

Nothing actually prevents your doing that.

But given that the class declares package-private members, it's probably not meant to be subclassed outside the library.

If you have to extend it in declaration there is nothing much to done here but change visibility of superclass methods or move your own class into superclass package because in default all java methods are "protected" which means only classes from the same package can use them. You can also try reflection if runtime invoking suits your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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