Comparison between C ++ inheritance and Java inheritance, and comparison between java inheritance
In C ++, inheritance can be divided into public inheritance (protected) and private inheritance (private ), in Java, there is only one inheritance by default (equivalent to the public inheritance in C ++). Let's look at a piece of code.
# Include <iostream> using namespace std; class A {int using vate; void using vate () {cout <"private method of class A" <endl;} protected: int aprotected; void Aprotected () {cout <"protected method of Class A" <endl;} public: A () {}a (int A, int B, int c ): initialize vate (a), aprotected (B), apublic (c) {} int apublic; void Apublic () {cout <"public method of class A" <endl ;}; // class B: private A {// public: // B () {}// B (int a, int B, int c): A (a, B, c) {}// void Bpublic () {// Aprotected (); // cout <"public method of Class B ----" <aprotected <endl; //}; int main () {A a (100,200,300);. apublic (); //. aprotected (); //. using vate (); cout <. apublic <endl; // cout <. aprotected <endl; // cout <. reset vate <endl; return 0 ;}
We have defined A class A, and we can see that:
1. Outside Class A, we can only access public members. Members of protected and private cannot access them.
2. the Class A internal member function can access all members, including (public, protected, and private ).
The above two features are exactly the same as those of Java.
Next we will let Class B inherit the sub-class A. The Code is as follows:
Class B: private A {public: B () {} B (int a, int B, int c): A (a, B, c) {} void Bpublic () {Aprotected (); cout <"public method of Class B ----" <aprotected <endl ;}};
Changing the above inheritance to private inheritance, protection inheritance, and public inheritance has the following conclusions:
1. subclass B can access non-private Members of the parent class.
2. Neither subclass B nor parent class A can be A private member of the parent class outside the class body.
3. For public inheritance, subclass B can access the public attributes (such as the Apublic () method) of parent class A from the outside ).
4. For private Inheritance and Protection inheritance, subclass B is not allowed to enumerate all attributes of Class A outside.
5. If it is to protect inheritance, both the protection attributes inherited from Class A and the Public attributes in Class B are changed to the protection attributes. Similarly, private inheritance applies.
The above descriptions may be somewhat bypassed. In fact, we can see the following conclusions from the above conclusions: inheritance methods (public, protected, private) this will change the access permissions (private, protected, and public) that the subclass inherits from the parent class attributes, as follows:
|
Public |
Protected |
Private |
Total inheritance |
Public |
Protected |
Invisible |
Private inheritance |
Private |
Private |
Invisible |
Protection inheritance |
Protected |
Protected |
Invisible |
From the above analysis, we can see that the inheritance method can change the subclass to inherit the access area of the sub-parent class attribute. Therefore, if subclass C inherits from Class B at this time, it also affects Class C's access to Class A members.
Let's take a look at the inheritance in Java. Let's take a look at the Code:
Public class A {private int implements vate; int afriendly; protected int aprotected; public int apublic; private void implements vate () {System. out. println ("private method of Class A");} void Afriendly () {System. out. println ("friendly Method of Class A");} protected void Aprotected () {System. out. println ("protected method of Class A");} public void Apublic () {System. out. println ("public method of Class ");}}
public class Test {public static void main(String[] args) {A a = new A();//a.Aprivate();a.Afriendly();a.Aprotected();a.Apublic();//System.out.println(a.aprivate);System.out.println(a.afriendly);System.out.println(a.aprotected);System.out.println(a.apublic);}}
You can see:
1. Java has one more friendly access permission than C ++. It should be a namespace that is not included in C ++ ).
2. The protected attribute in Java can be accessed outside the class.
3. The friendly attribute in Java can be accessed outside the class under the same package.
Note: The protected attribute can also be accessed in the same package class. If the Test Class and Class A are not in the same package, the preceding. afriendly () and. aprotected () is not accessible.
public class B extends A{public void Bpublic(){Afriendly();Aprotected();Apublic();}}
B b = new B();b.Bpublic();b.Afriendly();b.Aprotected();b.Apublic();
You can see:
1. Inheritance in Java is not differentiated (private inheritance and public inheritance). Therefore, after inheritance, it is equivalent to public inheritance in C ++.
2. The protected member in Java can be accessed outside the class (the premise of protected and friendly is in the same package ).
Let's take a look at the following features:
Public class B extends A {@ Overridepublic void Afriendly () {super. afriendly (); System. out. println ("override the Afriendly method of Class A");} public void Bpublic () {Afriendly (); Aprotected (); Apublic ();}}
You can see:
1. When re-writing the Afriendly method of Class A in subclass B, we can increase the access permission (as shown above)
2. There can only be one public class in a file in Java.
3. External classes in Java (corresponding to internal classes, not mentioned here) can only be modified to public or friendly, not private or protected.
Finally, let's make a summary:
1. There are four permissions in Java. public can be accessed throughout the project. The protected attribute can be accessed throughout the project of the class and subclass. The friendly attribute can be accessed throughout the project of the class, private can only be accessed within this class.
2. In C ++, the third permission is granted. The public permission can be accessed throughout the project. The protected attribute can only be accessed within the class and the subclass. The private attribute can only be accessed within the subclass.
In addition, C ++ can inherit more. An example of multi-inheritance is as follows:
# Include <iostream> using namespace std; class B1 {int b1; public: B1 (int I) {b1 = I; cout <"constructor B1." <I <endl ;}void print () {cout <b1 <endl ;}}; class B2 {int b2; public: b2 (int I) {b2 = I; cout <"constructor B2." <I <endl;} void print () {cout <b2 <endl ;}; class B3 {int b3; public: B3 (int I) {b3 = I; cout <"constructor B3." <I <endl ;}int getb3 () {return b3 ;}; class A: public B2, public B1 {int; b3 bb; public: A (int I, int j, int k, int l): B1 (I), B2 (j), bb (k) {a = l; cout <"constructor. "<l <endl;} void print () {B1: print (); B2: print (); cout <a <", "<bb. getb3 () <endl ;}}; int main () {A aa (1, 2, 3, 4); aa. print (); return 0 ;}