Inheritance is another comer stone of Object-Oriented Programming. Multiple inhe
ID: 3770868 • Letter: I
Question
Inheritance is another comer stone of Object-Oriented Programming. Multiple inheritance or its equivalent counterpart frequently appears in commercial codes. This question has the following setting of a multiple inheritance problem: This question consists of four following sub-questions: Implement the above inheritance problem in C++, and test the results in you main() routine; Implement the above inheritance problem in Java, and test the results in you main( ) routine; Implement the above inheritance problem in C#, and test the results in you Main() routine; Summarize what are your findings about the multiple inheritance of C++, Java, and C#. What are the similarity and difference between them?Explanation / Answer
C++:
#include <iostream>
#include<string>
using namespace std;
class Pianist
{
private:
string name;
public:
Pianist(string x)
{
name=x;
}
void pianoPlay()
{
cout<<"Play a piano!"<<endl;
}
};
class Violinist
{
public:
void violinPlay()
{
cout<<"Play a violin!"<<endl;
}
};
class Musician:public Pianist,public Violinist
{
public:
Musician(string name):Pianist(name),Violinist()
{
}
void pianoPlay()
{
cout<<"Play a violin!"<<endl;
}
void talk()
{
}
void sing()
{
cout<<"Sing a song !"<<endl;
}
};
int main()
{
Musician m("Musician 1");
m.sing();
m.pianoPlay();
m.violinPlay();
system("pause");
return 0;
}
Java:
class Pianist
{
private String name;
public Pianist(String x)
{
name=x;
}
public void pianoPlay()
{
System.out.println("Play a piano !");
}
}
interface Violinist
{
public void violinPlay();
}
class Musician extends Pianist implements Violinist
{
public Musician(String x)
{
super(x);
}
@Override
public void violinPlay()
{
System.out.println("Play a violin !");
}
public void sing()
{
System.out.println("Sing a song !");
}
}
public class InheritanceTest
{
public static void main(String arg[])
{
Musician m=new Musician("Musician 1");
m.sing();
m.pianoPlay();
m.violinPlay();
}
}
Output:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace inheritance
{
class Pianist
{
private string name;
public Pianist(string x)
{
name = x;
}
public void pianoPlay()
{
Console.WriteLine("Play a piano !");
}
}
interface Violinist
{
void violinPlay();
}
class Musician : Pianist,Violinist
{
public Musician(string x):base(x)
{
}
public void violinPlay()
{
Console.WriteLine("Play a violin !");
}
public void sing()
{
Console.WriteLine("Play a song !");
}
}
class InheritanceTest
{
static void Main(string[] args)
{
Musician m = new Musician("Musician 1");
m.sing();
m.pianoPlay();
m.violinPlay();
Console.Read();
}
}
}
Similarities and Differences:
->Multiple inheritance: Unline c++ C# and Java does not support multiple inheritance directly, but supports via interfaces.
-> interfaces: methods in the interface should not have body. Interface can be implemented in derived class only. But parent classes are implemented independently in c++.
->Super class constructor: super() method is used to call the super class constructor in java. Base() method is used to call super class constructor in C#.
Class name is used to call super class constructor in c++.
->Inheritance: extends keyword is used to inherit a parent class in java. Whereas, a simple colon followed by class or interface is used to inherit parent class in c++, c#.