If you want to add an additional function, you cannot do it by changing the existing code.
The ANdroid APK is read-only. You cannot write to it, so you cannot change the .java files in the APK.
However:
You can create a class that loads the new methods (NOTE Database, shared prefs or files is required) and strips the function of them, and takes appropriate action without writing the methods to the APK.
Essentially, you want to execute the method without writing it to the APK, so you need to design a class that can execute these methods.
OR:
YOu can create an external class(File only) in internal or external storage. Then you write any methods to that class. Refer this SO question to see how you can load and execute code in external .java files.
Please note that this is a workaround. You cannot write to the APK, but you can write .java files to the system and load them and execute them at runtime.
Final note
The first option (Create a class to load methods and execute them internally in the app) is extremely hard. It may even be impossible. The second option (where you create a .java file externally and load it in) is probably the easiest option around. You save a file externally and add any methods you want, and create a new instance of the external class in the class you want these methods in. This is the only way (I know of) you can create files and make them editable.
And I cannot say it enough times:
The APK is read only!
You cannot write to the APK, but you can read from it. For an instance oyu can read assets, but you cannot write to the assets.
The only way to "change" the contents of an APK is to decode the old, add whatever changes you need and create a new apk. But the process of doing this requires root, and adding the entire system to an application that does something else will add a lot of bloat to your app. Further, it will then most likely be signed with a different keystore and may make it incompatible with the store version, which may remove the option for updates in the future.
And from your comments, I apparently have to say it one more time:
You cannot change the APK like changing a text file! The APK is like a zipped file. To change it, you have to unpack it and then edit it. Meaning no matter what programming language you are thinking of, you have to unpack the APK, change it and repack it. Which, as I mentioned above, has many side effects.
And as you mentioned in another comment, if you suddenly are wondering about windows and the function there, that is a whole other topic (and a whole different set of developers who have competence on the topic). Ask another question instead of having two very distinct questions in one
If-elseorswitch-caseorDelegatesare the way to go here.