3

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?

dur
  • 13,039
  • 20
  • 66
  • 96
TheDahaka
  • 33
  • 6

2 Answers2

3

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.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • Thanks for you answer. This library I added to my project is read-only access, I suppose I can't add a java class to the package, can I ? – TheDahaka Aug 19 '16 at 09:10
  • @TheDahaka: I don't know what you mean by "read-only access" to a library. You can add a class to any non-restricted package you want. I *think* the only restricted package is `java.lang`, but I can't find that in the JLS. – T.J. Crowder Aug 19 '16 at 09:11
  • @T.J.Crowder can't we download the source jar, (like `jsoup-1.9.2-sources.jar`) and make changes in that? Is that valid? – Raman Sahasi Aug 19 '16 at 09:16
  • Sorry I'm not very at ease with this. I have attached the jsoup jar file to my project (working on Intellij), how can I find the org.jsoup.nodes package and write my class in it? – TheDahaka Aug 19 '16 at 09:17
  • @rD.: Well, you could, but then you'd have to keep your modified version in sync. There's no need for that when you can just put your own class in the package. – T.J. Crowder Aug 19 '16 at 09:17
  • @TheDahaka: I don't know the IntelliJ specifics, but you just create a `org/jsoup/nodes` folder and create your `MyNode.java` there, put the package declaration in it, and that's it. Just like putting your code in any other package. – T.J. Crowder Aug 19 '16 at 09:18
-2

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.

Michał Bil
  • 2,139
  • 5
  • 16
  • 23