Comparison of Inheritance in C++ and Java
The purpose of inheritance is same in C++ and Java. Inheritance is used in both languages for reusing code and/or creating an ‘is-a’ relationship. The following examples will demonstrate the differences Java and C++ provide support for inheritance.
1) In Java, all classes inherit from the Object class directly or indirectly. Therefore, there is always a single inheritance tree of classes in Java, and Object class is root of the tree. In Java, when create a class it automatically inherits from the Object class. In C++ however, there is a forest of classes; when we create a class that doesn’t inherit from another, we create a new tree in forest.
Following Java example shows that Test class automatically inherits from the Object class.
t is instanceof Object: true
2) In Java, members of the grandparent class are not directly accessible. See this G-Fact for more details.
3) The meaning of protected member access specifier is somewhat different in Java. In Java, protected members of a class “A” are accessible in other class “B” of same package, even if B doesn’t inherit from A (they both have to be in the same package).
For example, in the following program, protected members of A are accessible in B.
4) Java uses extends keyword for inheritance. Unlike C++, Java doesn’t provide an inheritance specifier like public, protected or private. Therefore, we cannot change the protection level of members of base class in Java, if some data member is public or protected in base class then it remains public or protected in derived class. Like C++, private members of base class are not accessible in derived class.
Unlike C++, in Java, we don’t have to remember those rules of inheritance which are combination of base class access specifier and inheritance specifier.
5) In Java, methods are virtual by default. In C++, we explicitly use virtual keyword. See this G-Fact for more details.
6) Java uses a separate keyword interface for interfaces, and abstract keyword for abstract classes and abstract functions.
Following is a Java abstract class example.
Following is a Java interface example
7) Unlike C++, Java doesn’t support multiple inheritance; A class cannot inherit from more than one class. However, A class can implement multiple interfaces.
8) In C++, the default constructor of the parent class is automatically called, but if we want to call parameterized constructor of a parent class, we must use Initializer list. Like C++, default constructor of the parent class is automatically called in Java, but if we want to call parameterized constructor then we must use super to call the parent constructor. See following Java example.
Base constructor called Derived constructor called
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.