2

I recently took a practice C# skills test and one of the questions was,

Does C# support multiple inheritance?

I answered yes, and was was marked wrong. After some research online, its full of answers of why it is not supported:

Multiple inheritance support in C#

Why is Multiple Inheritance not allowed in Java or C#?

http://www.codeproject.com/Questions/652495/Why-does-csharp-doesnt-support-Multiple-inheritanc

Then I went and tried to replicate the error I should be getting when trying to inherit from a Class that already inherited from a Base Class, and there is no error. I am using console application, and I recently upgraded to .net 4.5, maybe things have changed?

Code for how I tested:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            Leo bk = new Leo();

            bk.avgWords();

            Console.ReadLine();

        }

        public void bubbleSort(int[] input)
        {

        }

        public void insertionSort(int[] input)
        {

        }
    }

    public class Gatsby : Books
    {


        public override void avgWords()
        {

            Console.WriteLine(5);

        }

    }

    public class Leo : Gatsby
    {


        public override void avgWords()
        {
            Console.WriteLine(7);
        }

    }

    public class Dicaprio : Leo
    {


    }

    public class Books
    {

        public int id { get; set; }
        public string title { get; set; }
        public string author { get; set; }

        public virtual void avgWords()
        {


            Console.WriteLine(3);
        }


    }
}
2
  • 4
    That's not what multiple inheritance means. Commented Feb 12, 2014 at 1:23
  • This is Multi-level inheritance, not Multiple inheritance. Commented Feb 12, 2014 at 11:56

5 Answers 5

10

Then I went and tried to replicate the error I should be getting when trying to inherit from a Class that already inherited from a Base Class, and there is no error. I am using console application, and I recently upgraded to .net 4.5, maybe things have changed?

No, that is still considered single inheritance. Your class only inherits from a single base class.

Some languages, like C++, allow you to inherit from more than one class. The C# version would be something like:

class Foo {} 
class Bar {}

// This is invalid in C#!
class Baz : Foo, Bar {}

However, that's not allowed.

Note that C# does allow you to implement multiple interfaces, however.

Sign up to request clarification or add additional context in comments.

Comments

5

Does C# support multiple inheritance?

You cannot inherit from multiple base classes, but as with COM, you can re-use multiple base classes through multiple interfaces and containment and delegation:

// class C1 implements interface I1
interface I1
{
    void M1();
}

class C1 : I1
{
    public void M1() { Console.WriteLine("C1.M1"); }
}

// class C2 implements interface I2
interface I2
{
    void M2();
}

class C2 : I2
{
    public void M2() { Console.WriteLine("C2.M2"); }
}

// class C reuses C1 and C2
// it implements I1 and I2 and delegates them accordingly
class C: I1, I2
{
    C1 c1;
    C2 c2;

    void I1.M1() { c1.M1(); }

    void I2.M2() { c2.M2(); }
}

Comments

3

They probably meant can a class derive from two or more base classes, which it cannot. Confusing wording.

public abstract class A1;
public abstract class A2;
public abstract class B : A2;
public class C : A1, A2; // invalid
public class D : B; // valid

2 Comments

Not to nit pick too much, but "multiple inheritance" is a technical term, so the wording is not confusing, the asker simply isn't familiar with the term.
You are correct, but for most the question is more about knowing what the term "multiple inheritance" means and not the practice itself. If a simple pseudo code example was given, this person would not have incorrectly answered the question.
2

Multiple inheritance allows a class to inherit from more than one parent class. C# doesn't allow multiple inheritace; that does not mean a class' characteristics can be inherited only once.

It is possible to use multiple "implementation" through interfaces:

interface IInterface
{
    ...
}

interface IAnotherInterface
{
    ...
}

class MyClass : IInterface, IAnotherInterface
{
    ...
}

Comments

0

That is not considered as multiple inheretence but it is a multi-level or chain inheretence.

Multiple inheretence is when for example you have 3 classes A,B,and C. And class C is inheriting from classes A and B at the same time.

The problem with multiple inheretence and why it is not supported is that because if you have a method with the same name in both classes A and B with different implementation, and you want to use that method in class C, then class C will be confused of which of those method should it use. To prevent the confusion and ambiguity multiple inheretence is not supported in C#.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.