Application and difference of public, protected, and private in C ++ and Java

Source: Internet
Author: User

In Java, the three reserved words are used to modify the data members and member functions of the class, and the class itself. In C ++, the data members and member functions of the modifier class, and the inheritance of the class, but you don't need to modify the class itself.

◆ Application in C ++:
1. Public, private, and protected members

If the class keyword is used to define a class, any member before the first access label is implicitly specified as private. If the struct keyword is used, all the Members are public. The class or struct keyword is used to define the class, which only affects the default initial access level. By default (when no keyword is used), the struct member is public, and the class member is private.

· Public: the program can be used anywhere;

· Protected: It can only be accessed by the class itself and its derived classes and friends. In addition, a derived class can only access the protected member of its base class through the derived class object. The derived class has no special access permission to the protected member of its base class object. Like private, the object instance of the class is a member of the protected class that cannot be accessed.

For example, if bulk_item defines a member function that accepts a reference to a bulk_item object and a reference to an item_base object, this function can access the protected member of its own object and the protected member of the bulk_item parameter, however, it cannot access the protected member of the item_base parameter.

Void bulk_item: memfcn (const bulk_item & D, const item_base & B)
{
// Attempt to use protected member
Double ret = price; // OK: uses this-> price
Ret = D. Price; // OK: uses price from a bulk_item object
Ret = B. Price; // error: no access to price from an item_base
}

· PRIVATE: only the class itself can be accessed, as well as the membership functions and the membership class; the object instance of the class cannot access the private class members.

2. Inheritance of public, private, and protected

The base class itself specifies the minimum access control for its own members. If the member is private in the base class, only the friends of the base class and the base class can access the member. A derived class cannot access the Private Members of the base class, nor allow its users to access those members. If the base class member is public or protected, the access label used in the derived list determines the access level of the member in the derived class:

· If it is a public inheritance, the base class members maintain their own access level: the Public Member of the base class is the Public Member of the derived class, and the protected member of the base class is the protected member of the derived class.

· If it is protected inheritance, the public and protected members of the base class are protected members in the derived class.

· For private inheritance, all the members of the base class are private members in the derived class.

Assume that the size is public in base, but in derived, the size is changed to private after the base is inherited by private. To make the size public in derived, you can add a using declaration in the public part of derived. The definition of derived is changed as follows, so that the size member can be accessed by the user and the N can be accessed by classes derived from derived:
Class derived: Private base {
Public:
// Maintain access levels for members related to the size of the object
Using Base: size;
Protected:
Using Base: N;
//...
};

· Default inheritance Protection Level
The class defined by the reserved words of struct and class has different default access levels. Similarly, the default inherited access level defines different Derived classes based on the reserved words used. By default, class-based reserved word definitions have private inheritance, while struct-based classes have public inheritance by default.

· Friendship cannot be inherited
Let's take a look at the following code:
Class base {
Friend class frnd;
Protected:
Int I;
};
// Frnd has no access to members in D1
Class D1: public base {
Protected:
Int J;
};
Class frnd {
Public:
Int MEM (Base B) {return B. I;} // OK: frnd is friend to base
Int MEM (d1 d) {return D. I;} // error: Friendship doesn't inherit
};
// D2 has no access to members in base
Class D2: Public frnd {
Public:
Int MEM (Base B) {return B. I;} // error: Friendship doesn't inherit
};
As shown in the preceding figure, the Friends of the base class does not have special access permissions for the types derived from the base class. Similarly, if both the base class and the derived class need to access another class, the class must grant the access permission to the base class and each derived class.

· Inheritance and static members
If the base class defines static members, there is only one such member in the entire inheritance hierarchy. No matter how many derived classes are derived from the base class, each static member has only one instance. Static members follow regular access control: if the member is private in the base class, the derived class cannot access it. If you can access a member, you can access the static member either through the base class or through the derived class. Generally, you can use either the scope operator or the DOT or arrow member access operator.

Struct base {
Static void statmem (); // public by default
};
Struct derived: Base {
Void F (const derived &);
};
Void derived: F (const derived & derived_obj)
{
Base: statmem (); // OK: base defines statmem
Derived: statmem (); // OK: derived in herits statmem
// OK: derived objects can be used to access static from base
Derived_obj.statmem (); // accessed through derived object
Statmem (); // accessed through this class

· Accessibility of conversion from a derived class to a base class
If it is a public inheritance, both the user code and the descendant class can use the conversion from the derived class to the base class. If the class is derived using private or protected inheritance, the user code cannot convert the derived type object to the base class object. For private inheritance, the classes derived from the private inheritance class cannot be converted to the base class. If it is protected inheritance, the subsequent members of the derived class can be converted to the base class type.
No matter what the derived access label is, the derived class itself can access the public members of the base class. Therefore, the members and friends of the derived class can always access the conversion from the derived class to the base class.

◆ Applications in Java:

1. in Java, these three keywords are not as complex as C ++. When modifying data members and Member methods, the access permissions are as follows:

Private
It can only be accessed by methods of the class itself and internal classes.

 
Protected
It can be accessed by the class itself, subclass, and other classes in the same package.

 
Public
It can be accessed in all classes.
 

I plan to write another article to learn about the usage of internal classes separately.

2. Public modifier class itself

In a Java source file, there can be only one public class, but multiple non-public classes. The public class generally contains the main method and has the same name as the file name.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/chopin407/archive/2009/04/24/4107795.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.