Comparison of inheritance in C + + and inheritance in Java

Source: Internet
Author: User

In Java:

classparent{ Public voidTestinta) {System.out.println ("Parent:" +a); System.out.println ( This. GetClass (). GetName ()); }}classChildextendsparent{ Public voidTestintAintb) {System.out.println ("Child:" + A + "" +b); }} Public classParentandchild { Public Static voidMain (string[] args) {//TODO auto-generated Method StubChild ch =NewChild (); Ch.test (5);//in other words, the subclass of Java inherits the methods of the parent class .//The same name is overridden for the same parameter, and is overloaded for different parameters with the same name    }}
/*
Output Result:

Parent:5
Testone. Child

*/

In C + +:

#include <iostream>using namespacestd;classparent{ Public:

static void Run () {
cout<< "Parent" <<endl;
} voidTestinta) {cout<<"Parent:"<<a<<Endl; } voidTestintAintb) {cout<<"Parent:"<<a<<" "<<b<<Endl; }};classChild: Publicparent{ Public:

/* static void Run () {//Both of the two formulations are correct
cout<< "Child" <<endl;
}

void Run () {
cout<< "Child" <<endl;
}

*/

voidTestintAintb) {cout<<"Child :"<<a<<" "<<b<<Endl; }};intMain () {child ch; Ch. Parent::test (5,4);//This call provides access to the parent class's function with the same name//Ch.test (4);//Error: A function with the same name exists in the derived class and the base class, and the function in the base class is hidden, that is, it cannot be found//should be written in Ch. Parent::test (4);     return 0;} 

Difference: in C + +, overloading does not occur between the base class and the derived class! When a function of the same name exists in a base class and a derived class, a function with the same name in the derived class hides the function with the same name in the base class (hidden in the description below), regardless of the number of parameters or the same type of the function with the same name, instead of overloading the relationship. At this point, when you call the function with the same name through a derived class object, you can only access the function of the derived class, and if you want to access the function of the base class, you need to precede the function name with the class scope! As shown in the preceding code. In Java, the subclass inherits methods from the parent class, and the same name method in the child class is either overloaded with the same name method in the parent class, or is either an overwrite or an error (such as a different return type with the same argument)! )

hiding in C + + can only occur between a base class and a derived class, not within the same class (otherwise it causes the compiler to appear ambiguity). When a function of the same name exists in a base class and a derived class, regardless of the number of parameters or the same type of the function with the same name, the function of the same name in the derived class will hide the function of the same name in the base class, whether static or non-static , as in the above example, without overloading the relationship. At this point, when you call the function with the same name through a derived class object, you can only access the function of the derived class, and if you want to access the function of the base class, you need to precede the function name with the class scope! For virtual functions (which are decorated with virtual), if there is a virtual function in the base class, there is also a function with the same parameter in the derived class (then the function will be automatically falsified), then its return value must be the same as the return value of the virtual function of the base class! Otherwise hide failed!

But in Java, keep in mind the principle that non-static methods can only be overridden by (or by) non-static methods! Static methods can only be overridden by (or by) non-static methods! Abstract methods must be overridden in a specific class! The final method (the method with the keyword final) cannot be overwritten!

Comparison of inheritance in C + + and inheritance in Java

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.