Coders Packet

Multiple Inheritance in C++ and JAVA

By Kinjal Pagar

We are going to be focused on the topic - Multiple Inheritance in C++ and JAVA with the help of examples.

Today we will be going to look at the topic of Multiple Inheritance in C++ and JAVA.

 

The inheritance word states that something is inherited. The same thing is going to apply in the programming language. Where in the C++ and JAVA Inheritance is the process which gets or derives the property of the Main Class. There are various types of Inheritance including 

  • Single Inheritance
  • Multiple Inheritance
  • Multi-Level Inheritance
  • Hybrid Inheritance
  • Hierarchical Inheritance

But JAVA does not support Multiple Inheritance because to reduce the complexity and simplify the process. We will learn Multiple Inheritance with the help of example using JAVA and  C++. Below one shows C++.

#include 

using namespace std;
class Child_One
{
    public:
    Child_One()
    {
        cout<<"First Child";   
    }
};
class Child_Two
{
    public:
    Child_Two()
    {
        cout<<"\n Second Child";
    }    
};
class Parent: public Child_One, public Child_Two
{
    public :
    Parent()
    {
        cout<<"\n Parent Class.";
    }    
};
int main()
{
    Parent ObjectOfParent;

    return 0;
}

Here we can see there are two child classes and one parent class. Where the Parent class inherits the property of the child class. We can do this with the help of the following syntax :

Class : public , public , public

It says that we can inherit multiple child class in C++. But We can not do this in JAVA. Java supports single child inheritance as shown below example.

class Child_One
{
    public void Meth_Child_One()
    {
        System.out.println("First Child");
    }
};

class Parent extends Child_One
{
    public void Meth_Parent()
    {
        System.out.println("Parent Class");
    }
};
public class Main
{
  public static void main(String[] args) 
  {
    System.out.println("Main Class");
    Parent ParentObject = new Parent();
    ParentObject.Meth_Parent();
    ParentObject.Meth_Child_One();
  }
}
interface Child_One
{
    public void Meth_Child_One();
};
interface Child_Two 
{
    public void Meth_Child_Two();
};
class Parent implements Child_One, Child_Two
{
    public void Meth_Child_Two()
    {
        System.out.println("\n Second Class.");
    }  
    public void Meth_Child_One()
    {
        System.out.println("\n First Class.");   
    }
};
public class Main
{
    public static void main(String[] args)
    {
        Parent ParentObject = new Parent();
        
        ParentObject.Meth_Child_One();
        ParentObject.Meth_Child_Two();

    }
}

In this way, we have learned Multiple Inheritance in C++ and JAVA.

Thank You. Stay Safe.

You can use Interfaces and implement them in JAVA. Below is the example :

 

 

Download project

Reviews Report

Submitted by Kinjal Pagar (kirtypagar)

Download packets of source code on Coders Packet