Comments (4)
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.
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.
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.
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)
- PYP Q 2d HOT 2
- PYP last qn HOT 7
- Recitation 09-extra pdf not on luminus HOT 2
- PECS HOT 5
- QN5 17/18S1 [Why is A not the answer?] HOT 4
- Q5 HOT 3
- Can anyone explain what is a functor and a monad isπ HOT 8
- Mock Q4b HOT 9
- Impending P.A.N.I.C HOT 22
- AY19/20 SS1 qn 7 HOT 4
- Question on reduce function HOT 4
- AY18/19 Q2 Cyclical Dependency HOT 3
- error in obtaining int[] from a stream HOT 4
- Q3d) When I solved the game of Life problem for labs, I used loops. Can someone post their code using streams for 3d and 3e ? HOT 2
- Assertion questions HOT 2
- Mock 4a help HOT 13
- What kind of stream operations allow us to use .parallel() operation? HOT 4
- Final Submission Folder Disappeared HOT 6
- Anyone solved the RedLight , GreenLight, AmberLight problem? Can you please post your code? HOT 2
- excercise 2 inheritence question
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 2122-s2.