+ 1
can Setname function can be directly accessible from the main function?
is it possible that setname function can be access from the main, if yes then why we are using class for it. we should make an one function.
6 Answers
+ 2
setname function only allow you to change variable value.... in this case, you can put some restrictions for values to be set for protected variable.. let me give you some example..
for example, age is variable and anyone can set value as -1. instead of that, think about your function which check for this negative value and may be inform to user.
this is main advantage of oops specifically encapsulation... hih...
+ 6
setName() is to set the name for an object for whatever class you are making. As such, there is no point if you are not using any classes.
Also, I believe it *is* accessible from the main(), unless it's not public for some reason.
+ 5
@Mustafa Shaikh But if we make it public, outside possibly malicious code can directly interact with our variables and do unwanted things with them. But if we make it private but with a public setter function, then if you want to change it you have to use the setter method, so it is more secure.
+ 1
What are you talking about ?
+ 1
In my point of view, If we are making our data protected we shouldn't have to use setname function, Because in class we are making our data protected!
0
if we'll use setname function then data will be no more protected.