GithubHelp home page GithubHelp logo

Comments (4)

haoyangw avatar haoyangw commented on August 11, 2024

Private methods cannot be accessed or extended/inherited by any other class, so if your method is designed for use by other classes/clients, you probably don't want to declare it as private.

Protected methods can be called from any subclass as well, so if you want your methods to be accessible from any subclass, not just those in the same package, protected is suitable. E.g. if you want to declare methods that can be called/inherited and overridden by a subclass outside your package.

Public methods have no restrictions on access, so for security you should only declare a method as public when absolutely necessary(i.e. breaks code/programs when you use private/default/protected). Most of the time our code won't need public access right now anyway, so there's no benefit in declaring methods as public either.

Personally, I think choosing precisely the correct access modifier for your functions(rather than using only private/public) is helpful for you to kind of evaluate your program/code logic since it makes you think about/evaluate who should/shouldn't be running what code so you understand the code you're writing better too.

from 2122-s2.

jlee1007 avatar jlee1007 commented on August 11, 2024

Public methods can be accessible by everyone, within or outside the class, whereas private methods can only be used within the particular class where it is defined.

Protected methods provide a balance between private and public methods. Protected methods can be accessed within the class, or by its sub-classes, or classes within the same package. On the other hand, anyone outside of this scope will not be able to access the method e.g. Client won't be able to access the method. It is often used when you want to compare/ get information from different class objects without wanting to let your client see/access the code.

from 2122-s2.

Swagston20 avatar Swagston20 commented on August 11, 2024

Try not to use protected at all. This is because it gives access to propertiesand methods to other class within the same package which breaks the principle of immutability.

from 2122-s2.

weekiatism avatar weekiatism commented on August 11, 2024

To adhere to abstraction, having the appropriate access modifiers to ensure the hiding of certain methods and variables is needed.

from 2122-s2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.