“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”

The purpose of inheritance is the same in C++ and Java. Both languages use inheritance to reuse code and/or create “IS-A” relationships. The following example demonstrates the difference between Java and C++ in providing inheritance support.

1) In Java, all classes inherit directly or indirectly from the Object class. Thus, there is always a single inheritance tree of classes in Java, and the Object class is the root of the tree. In Java, when a class is created, it automatically inherits from the Object class. In C++, however, there is a forest-like; When we create a class that does not inherit from another, we create a new tree in the forest.

The following Java example shows that the Test class automatically inherits from the Object class.

  • Java
class Test {
	// Test members
}
class Main {
public static void main(String[] args) {
	Test t = new Test();
	System.out.println("T is an instance of the object:" + (t instanceofObject)); }}Copy the code

Output:

T is an instance of an object:true
Copy the code

2) In Java, members of a grandparent class cannot be accessed directly. See this G-FACT for more details. 3) The meaning of protected member access specifiers is somewhat different in Java. In Java, protected members of class “A” can be accessed in other classes “B” of the same package, even if B does not inherit from A (they must all be in the same package).

For example, in the following program, A protected member of A can be accessed from B.

class A {
   protected int x = 10, y = 20;
}

class B {
   public static void main(String args[]) {
   	A a = new A();
   	System.out.println(a.x + ""+ a.y); }}Copy the code

4) Java uses the extends keyword for inheritance. Unlike C++, Java does not provide inheritance specifiers like public, protected, or private. Therefore, we cannot change the level of protection for a base class member in Java, and if a data member is public or protected in the base class, it is still public or protected in a derived class. As in C++, private members of a base class are not accessible in a derived class.

Unlike C++, in Java we don’t have to remember inheritance rules that are a combination of base class access specifiers and inheritance specifiers.

5) In Java, methods are virtual by default. In C++, we explicitly use virtual keywords. See this G-FACT for more details. 6) Java uses the separate interface keyword for interfaces and the abstract keyword for abstract classes and functions.

Here is an example of a Java abstract class.

abstract class myAbstractClass {
	
abstract void myAbstractFun(a);

void fun(a) {
	System.out.println("Inside My fun"); }}public class myClass extends myAbstractClass {
public void myAbstractFun(a) {
	System.out.println("Inside My fun"); }}Copy the code

Here is an example Java interface

public interface myInterface {
void myAbstractFun(a); 
}

public class myClass implements myInterface {
public void myAbstractFun(a) {
	System.out.println("Inside My fun"); }}Copy the code

7) unlike C++, Java does not 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 called automatically, but if we want to call the parameterized constructor of the parent class, we must use the Initializer list. As in C++, the default constructor of the parent class is called automatically in Java, but if we want to call the parameterized constructor, we must use super to call the parent constructor. See the Java example below.

package main;

class Base {
	private int b;
	Base(int x) {
		b = x;
		System.out.println("Base constructor called"); }}class Derived extends Base {
	private int d;
	Derived(int x, int y) {
		super(x);
		d = y;
		System.out.println("Derived constructor call"); }}class Main{
	public static void main(String[] args) {
	Derived obj = new Derived(1.2); }}Copy the code

Output:

The base constructor of the call derives from the constructor callCopy the code