Question

If a developer extends a Java class that is part of the JDK and adds new methods to it, there is always the risk that a future version of java may introduce methods with the same name/signature resulting in unwanted behaviour if the program is executed with these future versions. Since there is no "Non-Overrides" annotation available (see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7152222) which would detect such possible problems when someone compiles the code with a newer version of the JDK, the developer has to do this checks in a different way. Which approach do you use?

Was it helpful?

Solution

It is an antipattern to extend JDK's classes, except those that are specifically designed to be used by extending them. You should use the Decorator pattern instead to add features to JDK classes. JDK is no special case, either, this holds for any 3rd party libraries as well.

OTHER TIPS

Inheritance is a powerful technique but due to lack of knowledge it can be worse. So In this case you have to use composition instead of inheritance. Create a class that contain the object of the any java class that you want to extend and then extend you class to that newly created class instead of the orignal class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top