-2

There are 'public', 'private', and 'protected' in oop like c++ language. And I tried two kinds of simple programs.

Below is first case in c++.

class A { 

public:
    string name;

}

int main(void) {

    A a;
    a.name;

}

And, second case...

class A { 

protected:
    string name;

public:
    string getName(void) {
        return name;
    }

}

int main(void) {

    A a;
    //a.name;    //can't access
    cout << a.getName();

}

Which one is better in two cases?

Because the information has to be hidden, the second one is maybe better, I think. But in second one, it can also access 'name' variable by using function getName(). If so, although the first one is simpler than second one, why should I use the second one? In other words, why is protected used?

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Jitoon
  • 3
  • 2
  • "Because the information has to be hidden, the second one is maybe better" - if hiding information is the goal, the second one is the *only* one that even remotely qualifies, so it wins "better" by default. – WhozCraig Sep 28 '16 at 06:37

3 Answers3

0

Second one is better class, we are encapsulate the data tightly to class and also allows the scope of inheritance by using protected. The member name can be changed only by member functions of class.

In first class, by making members as a public, we are allowing external functions from program manipulate the data which is not actually a good programming practice.

Prafull Ladha
  • 12,341
  • 2
  • 37
  • 58
0

Encapsulation.

In the first example anyone can use name in any way they wish. In this trivial an example they can't do much harm, but what if name is "Fred" and changing it to "Barney" will crash the program?

A.name = "Barney";

Program now crashes.

In the second example, name is inaccessible. getName returns a copy of name. The recipient can do anything they want to this copy without damaging the internal state of A, so

string temp = A.getName();
temp = "Barney";

does absolutely nothing.

Think of this as self defence for objects. Each object now has control over how its internal state is modified and can protect themselves from accidental misuse and damage.

The users of A don't even have to know how what they get from getName is stored. All they know is they get a string. This decouples A from it's users.

user4581301
  • 33,082
  • 7
  • 33
  • 54
0

Protected Access Modifier

The protected access modifier is accessible within package and outside the package but through inheritance only.

Private Access Modifier

The private access modifier is accessible only within class. Private is used basically for abstraction.

Example

package pack;  
public class A{  
protected void msg(){System.out.println("Hello");}  
}  
//save by B.java  
package mypack;  
import pack.*;  

class B extends A{  
  public static void main(String args[]){  
   B obj = new B();  
   A obj = new B();//compile time error
   obj.msg();  
  }  
}  
Output:Hello

private is more stricter than protected

Ketan G
  • 507
  • 1
  • 5
  • 21