By Delirium tremens

2009-06-20 02:08:31 8 Comments

What are public, private and protected in object oriented programming?


@monica 2011-04-11 11:53:45

All the three are access modifiers and keywords which are used in a class. Anything declared in public can be used by any object within the class or outside the class,variables in private can only be used by the objects within the class and could not be changed through direct access(as it can change through functions like friend function).Anything defined under protected section can be used by the class and their just derived class.

@Zaki 2009-06-20 09:44:36

To sum it up,in object oriented programming, everything is modeled into classes and objects. Classes contain properties and methods. Public, private and protected keywords are used to specify access to these members(properties and methods) of a class from other classes or other .dlls or even other applications.

@Ben S 2009-06-20 02:23:26

They are access modifiers and help us implement Encapsulation (or information hiding). They tell the compiler which other classes should have access to the field or method being defined.

private - Only the current class will have access to the field or method.

protected - Only the current class and subclasses (and sometimes also same-package classes) of this class will have access to the field or method.

public - Any class can refer to the field or call the method.

This assumes these keywords are used as part of a field or method declaration within a class definition.

@Landon Kuhn 2009-06-20 02:28:06

note that in java members of the same package can access protected members

@Ben S 2009-06-20 02:31:07

Yes, and Java also has a fourth access modifier which is the empty string. Not giving any access modifier will allow access from any package-level class.

@merkuro 2009-06-20 02:35:31

I like the "compiler" part, because most languages I know all functions/classes can be easily accessed at runtime for example via reflections in .NET. Therefore I tend to say, that these access modifiers are basically just a help for programmers to guide other programmers working on/with the same code by hiding certain things.

@Ben S 2009-06-20 02:38:39

@merkuro: I tried to be as concise as possible and hint towards that with the encapsulation mention.

@Fabio Gomes 2009-06-20 03:06:20

Some languages have some particularities. Like in Delphi, private members are acessible by other classes on the same unit, and you have to use strict private if you don't want this behavior.

@Ben S 2009-06-20 03:19:49

The question didn't mention a language so I tried to stay language-agnostic, though this is becoming a nice list of access modifier language differences :)

@Katerberg 2011-03-29 15:26:49

In addition to encapsulation, these also help implement inheritance relationships. Sorry for the necromancy.

@iGbanam 2012-11-23 08:16:43

C# also does the access-protected-members-of-same-package trick. In essence, C# is just Microsoft's Java

@Arthur Tacca 2016-12-09 09:04:13

Encapsulation is different from information hiding (that's why you had to link to a Wikipedia article with a different name), although they've gotten mixed up over the years. Encapsulation is just having methods and data together, even if they're all public. It's like they're together in a capsule, but no one said that the capsule can't be transparent! Data hiding is private members (and similar techniques).

@Uri 2009-06-20 02:11:31

They aren't really concepts but rather specific keywords that tend to occur (with slightly different semantics) in popular languages like C++ and Java.

Essentially, they are meant to allow a class to restrict access to members (fields or functions). The idea is that the less one type is allowed to access in another type, the less dependency can be created. This allows the accessed object to be changed more easily without affecting objects that refer to it.

Broadly speaking, public means everyone is allowed to access, private means that only members of the same class are allowed to access, and protected means that members of subclasses are also allowed. However, each language adds its own things to this. For example, C++ allows you to inherit non-publicly. In Java, there is also a default (package) access level, and there are rules about internal classes, etc.

@mnuzzo 2009-06-20 02:10:57

A public item is one that is accessible from any other class. You just have to know what object it is and you can use a dot operator to access it. Protected means that a class and its subclasses have access to the variable, but not any other classes, they need to use a getter/setter to do anything with the variable. A private means that only that class has direct access to the variable, everything else needs a method/function to access or change that data. Hope this helps.

Related Questions

Sponsored Content

47 Answered Questions

[SOLVED] What does "Could not find or load main class" mean?

  • 2013-08-07 03:02:46
  • Stephen C
  • 2745865 View
  • 1383 Score
  • 47 Answer
  • Tags:   java class main

28 Answered Questions

34 Answered Questions

[SOLVED] What exactly is RESTful programming?

  • 2009-03-22 14:45:39
  • hasen
  • 1659822 View
  • 3991 Score
  • 34 Answer
  • Tags:   http rest definition

16 Answered Questions

31 Answered Questions

32 Answered Questions

[SOLVED] What does it mean to "program to an interface"?

17 Answered Questions

[SOLVED] What is the difference between public, private, and protected?

19 Answered Questions

Sponsored Content