Questions tagged [public]

`public` is an access-specifier in object-oriented languages; it indicates that all objects have access to the `public` field or method.

In object-oriented languages, classes specify how much access other classes can have to their members.

When a member has the public specifier, other classes have full access to that member.

Other access specifiers are protected and private.

You can use this tag for questions about how the public specifier controls (or fails to control) access to the members of a class, and questions on how it interacts with the other access specifiers.

1633 questions
13
votes
4 answers

C++ why use public, private or protected inheritance?

Well there is enough information about this subject. For example this thread was very clear to me: Difference between private, public, and protected inheritance Except one point; Why is it useful?
Tim
  • 5,521
  • 8
  • 36
  • 69
13
votes
7 answers

What if main method is inside "non public class" of java file?

I have a java file containing more than one class, out of which one is public. If main method is inside a non-public class. I can't run that java file. Why is that? and there is no compilation error as well. If so, how can I use that main method?
Ahamed
  • 39,245
  • 13
  • 40
  • 68
13
votes
5 answers

javascript private function access public variable

i have this class: function ctest() { this.var1 = "haha"; this.func1 = function() { alert(this.var1); func2(); alert(this.var1); } var func2 = function() { this.var1 = "huhu"; } } and call it : …
MirrorMirror
  • 186
  • 8
  • 36
  • 70
12
votes
3 answers

Unity Doesnt Serialize int? field

I have a class i want to change the properties of in the editor. So i made my class System.Serializable and made the variables public that i want to be able to change. Like so: [System.Serializable] public class UIOptionsRing { public float…
FutureCake
  • 2,614
  • 3
  • 27
  • 70
12
votes
4 answers

swift: declare public variable

class XYActivity: UIActivity,YouTubeHelperDelegate { var youTubeHelper:YouTubeHelper var uploadURL: String! override init() { self.youTubeHelper = YouTubeHelper() } override func activityType() -> String? { …
rishu1992
  • 1,414
  • 3
  • 14
  • 33
12
votes
4 answers

Accessing "Public" methods from "Private" methods in javascript class

Is there a way to call "public" javascript functions from "private" ones within a class? Check out the class below: function Class() { this.publicMethod = function() { alert("hello"); } privateMethod = function() { …
mon4goos
  • 1,569
  • 13
  • 24
12
votes
1 answer

Need to declare a public instance variable in Objective-C

I'm trying to declare some instance variables for a custom button class in Objective-C (for iOS): @interface PatientIDButton : UIButton { NSUInteger patientID; NSString * patientName; } @end However, these are now private and I need them…
easythrees
  • 1,530
  • 3
  • 16
  • 43
12
votes
3 answers

How to access parent class's data member from child class, when both parent and child have the same name for the dat member

my scenario is as follows:: class Parent { public: int x; } class Child:public Parent { int x; // Same name as Parent's "x". void Func() { this.x = Parent::x; // HOW should I access Parents "x". } } Here how to access Parent's "X" from a…
codeLover
  • 3,720
  • 10
  • 65
  • 121
12
votes
5 answers

Difference between public static and private static variables

class Employee{ // salary variable is a private static variable private static double salary; // DEPARTMENT is a constant public static final String DEPARTMENT = "Development"; public static void main(String args[]){ salary = 1000; …
Android Girl
  • 2,074
  • 3
  • 22
  • 29
11
votes
4 answers

How to make all entities access:internal instead of public in EDMX?

I'd like my Entity Framework model to generate entities with internal access modifier, instead of public. I use the EF model in a library and I want only a single class (some controller) to be accessible from outside. Is there any simple way to…
Kornelije Petak
  • 9,412
  • 15
  • 68
  • 96
11
votes
6 answers

How to prevent an abstract class with public derived classes from being inherited in other assemblies?

I want to write something like the following: internal class InternalData { } public class PublicData { } abstract internal class Base { internal Base() { } private static InternalData…
penartur
  • 9,792
  • 5
  • 39
  • 50
11
votes
3 answers

Visual C# 2010 Express: Specify default access modifier for new classes?

Whenever I create new classes using Visual Studio 2010 Express C# it creates them with no access modifier. 9 times out of 10 I want my new classes to be public. How can I have Visual Studio create empty class templates with the "public" modifier by…
User
  • 62,498
  • 72
  • 186
  • 247
11
votes
6 answers

Should I put a public interface in a separate file?

I have the following code: import com.apple.dnssd.*; public interface IServiceAnnouncer { public void registerService(); public void unregisterService(); public boolean isRegistered(); } class HelloWorld { public static void…
Roman
  • 124,451
  • 167
  • 349
  • 456
11
votes
4 answers

How to access an object's public fields from a Velocity template

Here is my object class: public class Address { public final String line1; public final String town; public final String postcode; public Address(final String line1, final String town, final String postcode) { this.line1…
Alex Spurling
  • 54,094
  • 23
  • 70
  • 76
11
votes
3 answers

Accessing a member variable's address in derived class.Behavior change when member is having different access specifier

I have a base class A and derived class B class B is derived from A as public I want to access the member variable's address if A is class a is member variable I am observing different behavior when i am using protected and public access…
Astro - Amit
  • 767
  • 3
  • 15
  • 36