Inheritance In C++ Vs Java - C++ Programming Tutorial
C++ Course / Inheritance / Inheritance In C++ Vs Java

Inheritance In C++ Vs Java

BLUF: Mastering Inheritance In C++ Vs Java is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Inheritance In C++ Vs Java

C++ is renowned for its efficiency. Learn how Inheritance In C++ Vs Java enables low-level control and high-performance computing in the tutorial below.

In both C++ and Java, the objective of inheritance remains consistent. In both programming languages, inheritance is employed to recycle code and/or establish an 'is-a' relationship. Below instances will illustrate the distinctions between Java and C++ concerning inheritance support.

In Java, every class inherits from the Object class, either directly or indirectly. This establishes a singular inheritance hierarchy with the Object Class as the foundation. Conversely, in C++, classes form a diverse structure; creating a class without inheritance initiates a new branch in the class hierarchy forest.

The Test class automatically extends the Object class in Java, as demonstrated in the provided example.

Example

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

Output

t is instanceof Object: true

2) In Java, members of the grandparent class cannot be accessed directly. (For additional details, refer to this article.)

In Java, the protected access modifier functions uniquely. Protected members of a class "A" can be accessed by other classes "B" within the same package, regardless of whether B inherits from A or not.

In this particular program, for instance, protected attributes of class A can be accessed in class B.

Example

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);
	}
}

Output

For implementing inheritance, Java employs the 'extends' keyword. In contrast to C++, Java does not have inheritance specifiers like public, protected, and private. Consequently, in Java, it is not possible to modify the access level of base class elements; if a member is public or protected in the base class, it will retain the same access level in the derived class. Similar to C++, private members of a base class are inaccessible in derived classes.

In Java, unlike C++, there is no need to manually recall the inheritance principles, as it involves a base class access modifier and an inheritance modifier.

5) By default, virtual methods are inherent in Java, while in C++, we need to explicitly specify the virtual keyword (Refer to this article for further information).

6) Java utilizes the interface keyword for defining interfaces, whereas abstract keywords are employed in abstract classes and functions.

Here's an example of 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");
	}
}

An example of a Java interface is shown below.

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) In Java, multiple inheritances are not supported, distinguishing it from C++. This means a class cannot inherit from more than one other class. However, a class can implement multiple interfaces.

In C++, the parent class's default Object method is invoked automatically, while an Initializer list is required to call a parameterized Object method of the parent class. Similarly, in Java, the default Object method of the parent class is automatically invoked, but to call a parameterized Object method, the keyword super must be used. Below is an example in Java illustrating this concept.

Example

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

Output

Base constructor called
Derived constructor called

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience