-3

What is different Private data use get, set method and public data? For example:

Class Ex1
private int val = 0

private int getVal(){ return val }
==================================

Class Ex2
public int val = 0
===================================

If we want to reach val of Class Ex1, we make object and call getVal() method. However, val Class Ex2 just make object, and call that. Finally, they results are same, I know private , public protect used for secure, but I don't know what makes private has more secure than public.

Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
KIM
  • 233
  • 1
  • 3
  • 10
  • https://en.wikipedia.org/wiki/Encapsulation_(computer_programming) – OPK Dec 30 '15 at 16:22
  • 3
    [**Encapsulation**](https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)). – Elliott Frisch Dec 30 '15 at 16:22
  • *If we want to reach val of Class Ex1, we make object and call getVal() method.* `getVal()` on object of `Ext1` would give error, as you have defined it as private method. – Atri Dec 30 '15 at 16:32

2 Answers2

1

There are many differences which allow your class to change over time -

  • you can modify the getter method to return an immutable value or a copy without affecting clients.
  • you can modify the setter method to perform extra validation without affecting clients.
  • you can convert the field into some kind of derived field without affecting clients.
  • you can migrate the field into a nested class without affecting clients.

But really, you simply shouldn't be looking at object oriented programming in terms of holding data and exposing data - so you shouldn't be exposing getter / setter methods except in trivial cases (and even then it's debatable).

You should be thinking in terms of exposing methods, which represent behaviour, and hiding data, which allows behaviour to happen. In that case, there should be complex relationship between your data and your methods and a class's clients shouldn't really ever want access to the internal fields.

sisyphus
  • 6,174
  • 1
  • 25
  • 35
-1

The most usefulness of private is ability to make you program more clear to you and others. Use private everywhere you can. Private variables have a little scope so you can see all occurrences of it on one page of code, for an example.

Update

I think, there is all correct here. Scope of private variables is much more important than OOP garbage.

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • So what incorrect with my answer? I really can't understand. – v.ladynev Dec 30 '15 at 16:42
  • @BradLarson Ok. Thanks. – v.ladynev Dec 30 '15 at 21:07
  • It's not a big thing, but someone had flagged the answer as being offensive because of that wording. Just tweaked it a bit and removed the flag, but I can't speak for the other votes. – Brad Larson Dec 30 '15 at 21:09
  • What makes you think "secure" is not one of the reason of Encapsulation? You can do all kinds of validation in your setters before you assign the value to the variable, but without that there is no way you can do it. – OPK Dec 30 '15 at 21:10
  • @BradLarson Thanks a lot, man :) – v.ladynev Dec 30 '15 at 21:19
  • @JasonZ "Secure" is not a commonly used term. So my feel of it...class is a black box and you don't know how it performs tasks. But in a real life you need to know some details. But I am agree with your example and will edit my answer to except confusion. – v.ladynev Dec 30 '15 at 21:38