+ 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.

6th Feb 2018, 8:10 PM
Mustafa Shaikh
Mustafa Shaikh - avatar
6 Respuestas
+ 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...
3rd Apr 2018, 1:21 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 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.
6th Feb 2018, 11:02 PM
blackcat1111
blackcat1111 - avatar
+ 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.
7th Feb 2018, 6:05 AM
blackcat1111
blackcat1111 - avatar
+ 1
What are you talking about ?
6th Feb 2018, 9:58 PM
---
--- - avatar
+ 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!
7th Feb 2018, 1:07 AM
Mustafa Shaikh
Mustafa Shaikh - avatar
0
if we'll use setname function then data will be no more protected.
7th Feb 2018, 1:08 AM
Mustafa Shaikh
Mustafa Shaikh - avatar