answersLogoWhite

0


Best Answer

public members can be accessed from outside the class they were declared in, private members can only be accessed from within the class they were declared in. Private members are commonly manipulated through get/set methods, which allows for greater encapsulation and hides the implementation from the calling function.

Example:

class sampleClass{

private: int private_member;

public: int public_member;

public: void setPrivateMember(int x){private_member = x;}; // private members can be accessed from within the class that they are declared in

public: int getPrivateMember(){return private_member;};

};

int main()

{

sampleClass A;

A.public_member = 5; // Perfectly legal

A.private_member = 7; // Syntax error, this will not compile

A.setPrivateMember(7); // Legal

cout << A.getPrivateMember(); // Legal

return 0;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

A public member may be accessed by any class. A private member may only be accessed by the class in which it is contained.

Example:

// class A contains both a public int and a private int

class A {

public int publicNumber = 0;

private int privateNumber = 0;

}

// class B will attempt to access the members of A

class B {

void f() {

// Create a new instance of A

A a = new A();

// Try to print out each member

System.out.println(a.publicNumber);

System.out.println(a.privateNumber);

}

}

If you put each of these classes in a separate file and attempted to compile them, you would get an error informing you that 'privateNumber has private access in A'

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

A class member is simply the definition of a member, whether that member is a member variable or a member method. An instance member is the realisation of that definition, just as an object is an instance of a class.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The keyword private for the class is meant to be private to the class declare it. The private for the member is for the class contains that definition.

So the answer is a private class may have public, protected and private members.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

That depends on whether you mean a member variable or a member method. A class member variable is the definition of a type within a class. A class member method is the definition of a function within a class. You should know that objects are instances of a class, but each object has its own discrete set of member variables (including those inherited from base classes), each of which is an instance of the member variables defined by the class of the object, including its base classes. Member methods are different insofar as there is only one instance of each method per class, regardless of the number of objects instantiated from that class. Bear in mind that the entire concept of OOP is an abstraction to aid programming, so its easier to think that every object actually has its own discrete set of methods, when in fact they don't. They are really no different to structures and functions that act upon those structures, but the OOP concept ensures we don't call functions upon structures that have no business operating on those structures.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Private members are only accessible to the class itself and friends of the class.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Private means that the member can only be accessed by methods of the class, while public means that any code that has an instance of the class can access the member.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between public member and private member of a class in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between public and private in c plus plus?

A private member can only be accessed by other methods of the same class, while a public member can be accessed by methods of any class or by non class code.


The difference between private and public administration?

Public is open, private is closed.


What is the important difference between public and private administration?

Public is the opposite of private.


What is the difference between public and private sectors aims and objectives?

private anf public


What is the difference between a public bill and a private bill?

In Canadian Government, a private bill grants special powers or exemptions. ?&aelig;A Private member's bill is submitted for consideration by Parliament by a member who is neither a secretary nor Minister.


What are the Differences between public nuisance and private nuisance?

Well ask yourself what the difference between "public" and "private" is. This isn't rocket science!


What is a difference between public private partnership and private public partnership?

No, both refer to joint efforts by private companies and governmental bodies.


What is the difference between the private and public sectors of your economy?

Public sectors are funded by the government


What is difference between public and private bank?

public bank is state owned banks whereas private banks are owned by private individuals or entities.


What is the difference of public finance from private finance?

the different between public and private wants is on the financial means availlable and on the budgeting procedure


Difference between public and private estate?

Public is open to all for information but can be delt with the government.Private estate is more local to the area you live in.


What are the difference between public and private inheritance in c plus plus?

Public member, fields, methods etc can be accessed from outside of the class. While private members etc can accessed only within the class even "child" classes do not have access to private members, fields etc.