Inheritance in C++ vs Java
Just like we inherit traits from our parents, object-oriented programming has a concept called inheritance. In terms of object-oriented programming, a class's traits and behaviours, or its data and methods, are referred to as its characteristics.
As it allows objects to take on the characteristics and behaviours of other objects, inheritance is a key component of object-oriented programming. As a result, less new code needs to be created, written, and tested every time a new programme is created. Classes inside a programme inherit from one another, creating a hierarchical connection.
Inheritance comparison in C++ and Java :
Inheritance serves the same function in Java and C++. Both languages utilise inheritance to reuse code and/or establish a "is-a" connection. The distinctions among Java and C++ that enable inheritance will be shown by the examples that follow.
1.In Java, all the classes either directly or indirectly are derived from the Object class.
As a result, there is always only one class inheritance tree in Java, with the Object Class acting as its root. When a class is created in Java, the Object Class is immediately inherited. A class that doesn't inherit from another is created as a new tree in a forest in C++, where there is a forest of classes.
The Tests class automatically derives from the Object class, as seen by the Java example.
Example :
class Tests {
// members of the tests class
}
class Main {
public static void main(String[] args)
{
Tests ts = new Tests();
System.out.println("ts is an instance of Object: "
+ (ts instanceof Object));
}
}
Output :
ts is an instance of Object: true
2. In Java, it is not possible to directly access members of the grandparent class.
3. In Java, protected member access specifier has a somewhat different meaning.
In Java, even if class "Q" doesn't inherit from class "P," protected elements of class "P" are available in class "Q" of the same package (both of them must need to be in the same package).
For instance, in the programme below, protected members of P are reachable in Q.
Example :
class P {
protected int i = 100, j = 200;
}
class Q {
public static void main(String args[])
{
P p = new P();
System.out.println(p.i + " " + p.j);
}
}
Output :
100 200
4. For inheritance, Java utilises the 'extends' keyword.
Java does not include inheritance specifiers like public, protected, or private, in contrast to C++. Because of this, we are unable to reduce the security level of base class members in Java. If a data item is public or protected in the base class then it will stay the same in the derived class as well. Similar to C++, a derived class's private members cannot be accessed.
Contrary to C++, Java does not need us to know the inheritance rules, which combine the inheritance specifier and base class access specifier.
5) In Java, virtual methods are the default while virtual keyword is used explicitly in C++.
6) Java employs the keywords interface and abstract, respectively, for the interfaces and for the classes and methods that are abstract.
Given below is an instance of abstract class in java.
Example :
// An example of the abstract class
abstract class theAbstractClass {
// An abstract method
abstract void theAbstractFun();
// A normal method
void func() { System.out.println("Inside the func"); }
}
public class theClass extends theAbstractClass {
public void theAbstractFun()
{
System.out.println("Inside the func");
}
}
Given below is an instance of java interface.
Example :
// An example of java interface.
public interface theInterface {
// theAbstractFun() is public
// and abstract, even if we
// don't use these keywords
void theAbstractFun();
// is same as public abstract void theAbstractFun()
}
// implements keyword is also used.
public class theClass implements theInterface {
public void theAbstractFun()
{
System.out.println("Inside the func");
}
}
7) Java does not enable multiple inheritances, in contrast to C++.
A class can only descend from one other class. A class, though, can implement several interfaces.
8) In C++, the parent class's default constructor is automatically called, but if we wish to invoke a parent class's parameterized constructor, we must utilise the Initializer list. In Java, the parent class's default constructor is automatically invoked, similar to C++, but if we wish to execute a parameterized constructor, we must use the “super” keyword. Refer to the Java instance shown below.
Example :
class Base {
private int B;
Base(int p)
{
B = p;
System.out.println("The Base constructor is called");
}
}
class Derive extends Base {
private int D;
Derive(int p, int q)
{
// Parent class parameterized constructor is being called
// The calling of parent constructor must be in the first line
// inside the Derived class 'Derive'
super(p);
D = q;
System.out.println(" The Derived constructor is called");
}
}
class Main {
public static void main(String[] args)
{
Derive objct = new Derive(10, 20);
}
}
Output :
The Base constructor is called
The Derived constructor is called