Home » Inheritance in C++ vs JAVA

Inheritance in C++ vs JAVA

by Online Tutorials Library

Inheritance in C++ vs JAVA

In both C++ and Java, the purpose of inheritance is the same. In both languages, inheritance is used to reuse code and/or create a ‘is-a’ relationship. The following examples will show the differences between Java and C++ in terms of inheritance support.

1) In Java, all classes either directly or indirectly inherit from the Object class. As a result, in Java, there is always a single inheritance tree of classes, with the Object Class at the root. When you create a class in Java, it automatically inherits from the Object Class. However, in C++, there is a forest of classes; when we create a class that does not inherit from another, we are creating a new tree in the forest.

The Test class inherits from the Object class by default, as shown in the Java example.

Java

Output

t is instanceof Object: true  

2) Grandparent class members are not directly accessible in Java. (For more information, see this article.)

3) In Java, the protected member access specifier has a slightly different meaning. Even if B does not inherit from A, protected members of a class “A” are accessible in other classes “B” in the same package in Java (they both have to be in the same package).

In the following programme, for example, protected members of A are accessible in B.

Java

Output

10 20  

4) For inheritance, Java uses the ‘extends’ keywords. Unlike C++, Java lacks inheritance specifiers such as public, protected, and private. As a result, in Java, we can’t change the protection level of base class members; if a data member is public or protected in the base class, it will remain public or protected in the derived class. Private members of a base class are not accessible in derived classes, just like they aren’t in C++.

Unlike C++, we don’t have to remember the inheritance rules in Java, which are made up of a base class access specifier and an inheritance specifier.

5) In Java, virtual methods are the default. We use virtual keywords explicitly in C++ (Refer to this article for more details).

6) For interfaces, Java uses the interface keyword, while abstract keywords are used for abstract classes and abstract functions.

Here’s an example of a Java abstract class:

Java

An example of a Java interface is shown below.

Java

7) Java does not support multiple inheritances, unlike C++. A class can’t be descended from more than one other class. A class, on the other hand, can implement multiple interfaces.

8) In C++, the default function Object() { [native code] } of the parent class is called automatically, but we must use the Initializer list if we want to call a parameterized function Object() { [native code] } of a parent class. In Java, the parent class’s default function Object() { [native code] } is called automatically, just like in C++, but if we want to call a parameterized function Object() { [native code] }, we must use super to call the parent function Object() { [native code] }. Take a look at the Java example below.

Java

Output

Base constructor called  Derived constructor called  

You may also like