DEV Community

Cover image for Java call a method from another class.
jacob777baltimore
jacob777baltimore

Posted on

Java call a method from another class.

Java can call a method from another class by using the behavior and the access level is present there. Following access modifiers can be used within the methods.

Access level / Modifiers:

Public:

In public modifiers, methods are visible to any other class you are using. The users can access from anywhere as long as they create a new instance of the class otherwise the user will get NullPointerException.

Private:

In private modifier, methods are only visible within that class. Methods are only available within the method that is encapsulated in that class.

Protected:

In protected access modifier is limited to package. The users can get the ability for their subclass outside the superclass package by using the to inherit methods and constructors. The users can only access by using the inheritance. In this way methods only extend the superclass functionalities not replace it. Read more

Top comments (0)