Comparison of C + + inheritance and Java inheritance

Source: Internet
Author: User

Inheritance in C + + can be divided into public inheritance (common) protection inheritance (protected) and private inheritance (private), whereas in Java there is only one inheritance (equivalent to public inheritance in C + +) Let's look at a piece of code

#include <iostream>using namespace Std;class a{int aprivate;void aprivate () {cout << ' private method of Class A ' << Endl;} Protected:int aprotected;void aprotected () {cout << "A-Class protected method" << Endl;} Public:a () {}a (int a, int b, int c): Aprivate (A), aprotected (b), Apublic (c) {}int apublic;void apublic () {cout << "Class A P Ublic method "<< 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 (+, +); A.apublic ();//a.aprotected () ;//a.aprivate (); cout << a.apublic << endl;//cout << a.aprotected << endl;//cout << A.aprivate << Endl;return 0;}
We define a class A, and we can see:

1. outside Class A we can access only public members , protected and private members are inaccessible.

2. The internal member function of Class A can access all members , including (public, protected, private).

The above two features are exactly the same as Java.

Let's make Class B inherit the subclass a class with the following code:

Class B:private A{public:b () {}b (int A, int b, int c): A (A, B, c) {}void bpublic () {aprotected (); cout << "Class B public Method- ---"<< aprotected << Endl;};
To change the above inheritance to private inheritance, protection inheritance, public inheritance, there are the following conclusions:

1. Within subclass B , the non-private members of the parent class can be accessed.

2. Subclass B and parent Class A are not accessible to private members of the class outside the class body.

3. In the case of public inheritance, subclass B can access the public properties of parent Class A (such as the Apublic () method) externally.

4. If private inheritance and protection are inherited, subclass B is externally inaccessible to all properties of Class A.

5, if the protection of inheritance, in Class B inherits from the Class A protection and public properties are protected properties, the same as private inheritance.

These descriptions may be somewhat around, in fact, we can see from the above conclusion that the inheritance (public, protected, private) will change the subclass inherits from the Parent property's access rights (private, protected, public), as follows:

is not visible is not visible
&NBSP, public protected private
Total inheritance public protected
private inheritance private private
Protect inheritance Protected Protected Not visible

From the above analysis, we can see that inheritance can change the subclass inherits the access area of the parent class property, so if there is subclass C inheriting from Class B, it will also affect the class C access to Class A members.

Let's take a look at the inheritance in Java and look at the code first:

public class A {private int aprivate;int afriendly;protected int aprotected;public int apublic;private void Aprivate () {Sys Tem.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 (the public method of Class A);}}
public class Test {public static void main (string[] args) {A A = new A ();//a.aprivate (); a.afriendly (); a.aprotected (); A.apu Blic ();//system.out.println (a.aprivate); System.out.println (a.afriendly); System.out.println (a.aprotected); System.out.println (A.apublic);}}
can see:

1, Java more than C + + a friendly access rights, should be in C + + without the concept of package (is the namespace).

2. The protected property in Java can be accessed outside the class.

3. The friendly property in Java can be accessed outside the class under the same package.

Note: The protected property can also be accessed in the same package class, if the test class and Class A are not under the same package, a. Afriendly () and a. Aprotected () are 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 ();

can see:

1. Inheritance in Java is not distinguished (private inheritance and public inheritance), so inheritance is equivalent to public inheritance in C + +.

2. Protected members in Java can be accessed outside the class (protected and friendly are provided under the same package).

Let's look at one more feature:

public class B extends a{@Overridepublic void afriendly () {super. Afriendly (); System.out.println ("Overriding the afriendly method of Class A");} public void Bpublic () {afriendly (); Aprotected (); Apublic ();}}
can see:

1, in sub-class B to rewrite the Class A afriendly method, we can increase access permissions (such as public above)

2. There can be only one public class in a file in Java.

3, the external class in Java (corresponding to the internal class, here for the moment) can only be modified to public or friendly, can not be modified to private or protected.

Finally, let's make a summary:

1, there are four kinds of permissions in Java, public can access throughout the project, the protected property can be accessed throughout the project in this class and subclass, the friendly property can be accessed throughout the project in this class, private only within this class.

2, in C + + three permissions, public can be accessed throughout the project, the protected property can only be accessed inside the class and within the subclass, the private property can only be accessed within the subclass.


Comparison of C + + inheritance and Java inheritance

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.