Try / catch with a private method [closed]

Multi tool use
Multi tool use


Try / catch with a private method [closed]



I am trying to call a method that is private inherited from the super class (I KNOW THAT YOU CAN NOT CALL IT IN MAIN BECAUSE IT IS PRIVATE) However, I am trying to use a try catch statement that keeps running my program and give me an exception saying "This method cannot be Called because it is private" (EXAMPLE)



this is what I have on my try / catch


public int num1 = 3;

try
{
superClass.methodOne(num1);
}
catch(Exception e)
{
System.out.printf("%s","ERROR: methodOne cannot be executed! IT IS PRIVATE");
}



This is the error that it gives me!



error: methodOne(int) has private access



Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





What about this error message confuses you? You seem to already understand that you can't directly call a private method from a subclass.
– Tim Biegeleisen
Jul 2 at 1:48





I understand it, but I am calling other methods in the program and I just want them to know that specific method can not be called.
– Richard Orfao
Jul 2 at 1:51





private methods aren't visible in inherited classes. They already know that specific method can't be called. It isn't visible.
– Elliott Frisch
Jul 2 at 1:52


private





This is very confusing. You know the private method can't be called. Java knows the private method can't be called. Are you asking what the name of the Exception is when trying to call the private method? Because there wouldn't be one; you will get a compiler error, not a run-time error.
– Zephyr
Jul 2 at 1:54





You should not even be able to compile, let alone start, your application...
– Zephyr
Jul 2 at 1:55





1 Answer
1



What's good about Java is that it is a compiled language, meaning that it takes your code and prepares a special bytecode (a set of internal instructions that are executed by the program called Java Machine), before executing.



This compiling phase eliminates a huuuge number of mistakes even before you try to run a program. Example, when you make a mistake, like trying to assign a number to a String variable, the compiler cannot translate it to bytecode (its internal language) for execution, because a number is not a String. It was just not taught to do that.



In the same manner, when you tell that a method is "private", it means that it can be used only within your class. For other classes, it will be a completely invisible, as if it didn't exist.



Therefore, when you write superClass.methodOne(...) where methodOne is a private method of another class, it means that this method is hidden within that super class, and it doesn't exist for the subclasses.


superClass.methodOne(...)



It will be a contradiction, telling the Compiler to create an instruction to a hidden method, an impossible task. With the same "success", you could write superClass.blablabla(...); or superClass.bruhbruhbruh(...), or superClass.guubledeebubblegup(...).


superClass.blablabla(...);


superClass.bruhbruhbruh(...)


superClass.guubledeebubblegup(...)



If these methods don't exist objectively, the private (=hidden) method formally doesn't exist either for the subclass. Therefore, Java Machine cannot convert it to the bytecode, or even try to use a method that formally doesn't exist for the subclass

0eZ DbTzlPGj3 U zv LOtyYMDehJo5qDpRB0lwHcY5zXYYzIMHgd,D
WM7s9,K JacDMFZjF9Pa BBOE7G q0NxhzH8fH6zWIDKIhma xfhTbKC5LQdQ,1L k3vvPy7pnw62a

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications