简体   繁体   中英

Why does final class WebView contain protected method getChildren() in JavaFX?

WebView class is final. It contains method getChildren() , which is protected.

How to use it then?

UPDATE

The question is not about protected adn/or final keywords, but about why this is done this way? What is the reason to have protected members in final classes?

protected means "package and inheritance access" so classes in the same package can access this method.

EDIT:

answer to your edit: it's because designers of this class wanted this method to be available ONLY to classes within the same package and no other classes (thus you cannot extend it because their made it final)

When it declared as protected, Still the classes who instantiate the WebView class in the same package can use that method.

You might think that it cannot be extended as it is final , there is no way to access it outside of the class. Yes, that is true but with in the package, they can.

Modifier    Class   Package Subclass    World
---------------------------------------------
protected   Y      **Y**        Y           N

As you know, final classes cannot be extended. A final class can not be subclassed.

protected Modifier - Accessed by other classes in the:

  • same package
  • any subclasses of the class in which they are referred (ie same package or different package ).

You can't extend it because it's final.

  • Create and instance of Webview class and call newWebViewInstance.getChildren() .
  • You can create a package with the same package of Webview, but anybody with right sense of Java programming language wouldn't do it.

Update

I don't like to make member/methods of a final class protected . IMHO that is bad programming practise unless in this case.

Why was it made protected?

WebView extends javafx.scene.Parent

And getAllChildren() is an method of javafx.scene.Parent which is of protected scope.

protected javafx.collections.ObservableList<javafx.scene.Node> getChildren()

As we now know Webview is a subclass of javafx.scene.Parent and overrides getChildren() . If it changes the scope (default being stricter than protected) it breaks the overriding contract.

Hope it's clear now.

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