The purpose and the application of the inheritance is similar in both java and c++.
Inheritance is used on the purpose of reusing the code and creating the relationship between the classes and interfaces.
There are following differences in the way both languages provide support for inheritance.
In java, classes inherit directly from Object class directly
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.
Following Java example shows that Test class automatically inherits from Object class.
1 2 3 4 5 6 7 8 9 10 11 | 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)); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 | 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); } } |
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.
Java uses a separte keyword interface for interfaces, and abstract keyword for abstract classes and abstract functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // An abstract class example abstract class myAbstractClass { // An abstract method abstract void myAbstractFun(); // A normal method void fun() { System.out.println("Codingsec is fun"); } } public class myClass extends myAbstractClass { public void myAbstractFun() { System.out.println("Codingsec refers to be coding with security !"); } |
Unlike C++, Java doesn’t support multiple inheritance. A class cannot inherit from more than one class. A class can implement multiple interfaces though.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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); } } |
Comment down below if you find anything incorrect or inappropriate. I feel responsible for what I write.