I give a response that is not exactly on the point. Of course you can use assert in public methods (or wherever you like).
The point is more on what you should do or not. I myself perfectly understand other people's response about when you should or shouldn't use assertion.
But I must admit that I NEVER use assertions, and that I rarely if ever see assertions in code. I worked only for a few years, but on the 4 totally different companies I worked for, there were no assertions in code. Be it the more than 10 millions line of code web application to book flights, a spacecraft control center, a software to manage hypermarket or a bug tracker. None of them used asserts. All companies had different needs and methods. None used asserts.
For me the reason is simple. People here say that assertions are for debugging. That's fine. And that you can deactivate them for improved speed. That fine too... At first. The more complex the program, the more you pass time debugging. And some bugs, even with 100% code coverage, even with extensive integration and validation testing, you'll find them only in production. Simply because your users end up using your application more than you. And they will not use it the same way as you.
It's funny because in production logs, we keep seeing stacktraces from code like this :
catch (MyException e) {
logger.war("This should never happen",e);
}
What that means is that you never know what could occur in production.
And that if you have the opportunity to make a check, do it. Of course the log comment is more funny than useful here and it would be better to let the exception popup.
In all cases don't make it an assertion that will be disabled in production. Because it will be useless. Make it normal code that raises an exception. Ensure it is logged if appropriate. Ensure that an error will be displayed on the UI. And ensure you be able to get the exception and the logs to investigate.
What is important is that one day or the other, some user will do things that make the warning popup. Be it because of a badly written code or anything, you'll see it. And that means that instead of spending 2 days finding why in hell the program has this strange behavior, you'll be able to use the full stacktrace at a starting point and correct the problem in 2 hours.
A check with a disabled assert is the same as no check at all. It is code you must write, read and maintain... For nothing. I understand the whole performance argument where assertions in production slow things down. Yes in some few case, there is a performance problem. In most case, you'll gain almost nothing anyway and lost precious hints.