Home > Software design >  How the method getDefaultToolkit works in java?
How the method getDefaultToolkit works in java?

Time:03-23

I'm learning Java, particularly how to make GUIs. I don't understand how the method getDefaultToolkit works, because this method is abstract and I have been looking in the API and it seems that no class inherits from Toolkit because. In some classes it appears the "Direct Known Subclasses" section but in the class Toolkit it doesn't appear.

Thanks!

CodePudding user response:

I have been looking in the API and it seems that no class inherits from Toolkit because. In some classes it appears the "Direct Known Subclasses" section but in the class Toolkit it doesn't appear.

You have the right idea. There is some class that extends Toolkit and implements all of the necessary methods. This class is not published as part of the public API. This means that the exact name of this class is hidden. Honestly those details aren't really important. That's the whole point of inheritance and polymorphism.

getDefaultToolkit() creates an instance of this class and returns it.

CodePudding user response:

The class Toolkit is abstract, the method getDefaultToolkit() is not abstract, it is even static. So you absolutely do not need a specific subclass to invoke it, to the contrary. As the method is static, the "correct" way to invoke it is by simply calling Toolkit.getDefaultToolkit(); thus there is no need at all for any known subclass to implement anything here.

That is all there is to this.

Well: the fact that the OP couldn't find any subclass of Toolkit is based on misconception: not every class that gets shipped with Standard Java is documented.

  • Related