The purpose of inheritance is same in C++ and Java. Inheritance is used in both languages for reusing code and/or creating is-a relationship. There are following differences in the way both languages provide support for inheritance.

Java和C++的采取继承的目的都是为了提供了一种类之间的从属关系

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, if we create a class that doesn’t inherit from any class then it automatically inherits from Object class . In C++, there is forest of classes; when we create a class that doesn’t inherit from anything, we create a new tree in forest.

与c++不同的是,java所有类都直接或间接继承自Object这个基类,因此对于java的类一直会有一棵单根的继承树存在,而Obejct类就是这棵树的根,若在Java中我们创建一个类不继承任何其他类那么它默认就已经继承了Object类了;C++没有这种的机制,如果一个类不继承任何类那么它真的就是不继承任何类,因此每次我们创建一个新类,在C++中相当于种了一棵新的树木,而在java中相当于只是生长了新的枝干。

Following Java example shows that Test class automatically inherits from Object class.

class Test {
     // members of test
}
class Main {
   public static void main(String[] args) {
     Test t = new Test();   
     System.out.println( "t is instanceof Object: " + (t instanceof Object));
   }
}

Output:

t is instanceof Object: true



2) In Java, members of the grandparent class are not directly accessible. See this G-Fact for more details.

在Java中,只有自己的直接父类可以被访问,祖父类不能直接被访问。

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.

Java中的protected的意义和c++中略有不同,java中A类的protected属性可以被同属一个包内的B类所访问,就算B不继承自A,但至少A和B必须在同一个包内。

// filename B.java
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);
     }
}



4) Java uses extends keyword for inheritence. 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.


与C++不同的是,JAVA并不提供继承关键字,因此在继承过程中我们无法改变基类成员已有的访问保护级别;



5) In Java, methods are virtual by default. In C++, we explicitly use virtual keyword. See this G-Fact for more details.

在Java中,类的方法默认就是虚函数,然而在c++中我们必须显式地定义才行。

6) Java uses a separte keyword interface for interfaces, and abstract keyword for abstract classes and abstract functions.

在Java中定义接口使用interface关键字,定义虚类使用abstract关键字。

Following is a Java abstract class example.

// An abstract class example
abstract class myAbstractClass {
    
    // An abstract method
    abstract void myAbstractFun();
   
    // A normal method
    void fun() {
       System.out.println( "Inside My fun" );
    }
}
 
public class myClass extends myAbstractClass {
    public void myAbstractFun() {
       System.out.println( "Inside My fun" );
    }
}

Following is a Java interface example

// An interface example
public interface myInterface {
    // myAbstractFun() is public and abstract, even if we don't use these keywords
    void myAbstractFun();  // is same as public abstract void myAbstractFun()
}
 
// Note the implements keyword also.
public class myClass implements myInterface {
    public void myAbstractFun() {
       System.out.println( "Inside My fun" );
    }
}



7) Unlike C++, Java doesn’t support multiple inheritance. A class cannot inherit from more than one class. A class can implement multiple interfaces though.

Java不支持多继承,但支持实现多个接口


8 ) In C++, default constructor of parent class is automatically called, but if we want to call parametrized 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 parametrized constructor then we must use super to call the parent constructor. See following Java example.

C++中的父类构造函数是会自动被调用的,但如果我们想要呼叫一个带参数的父类构造函数,我们必须要使用初始化列表。

如C++一样,默认父类构造函数在Java中也会被自动调用,但如果我们想要呼叫一个带参数的父类构造函数,必须使用super

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) {
         // Calling parent class parameterized constructor
         // Call to parent constructor must be the first line in a Derived class
         super (x);
         d = y;
         System.out.println( "Derived constructor called" );
     }                   
}
  
class Main{
     public static void main(String[] args) {
       Derived obj = new Derived( 1 , 2 );
     }
}

Output:

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.