How come c# don't allow multiple inheritance?

  • Thread starter Thread starter Patrick F
  • Start date Start date
P

Patrick F

Hi,

How come that a programming language not yet 10years old dosen't support
Multiple inherritances?, I got 2 classes, both with alot of fields and
methods that i need from my third class, so i tried:

class myClass3 : myClass1, myClass2 {

and got an error says that i cant. How can C# be a compete with C++ if
you cant even make a simple thing like that?
Is there a work around?

Patrick
 
Interfaces from what i have seen cant contain Fields, and both my
classes that i am using contains fields. I have it like this (just for
demonstration):

A - Car class (alot of fields for data)
AC - Array of A (field) that is dynamic so i can add and remove Array
elements (like a que/stack), alot of methods for extracting data from A

B - Bus class (alot of fields for data)
BC - Array of B (field) that is dynamic so i can add and remove Array
elements (like a que/stack), , alot of methods for extracting data from
B

C - Traffic class, witch needs both AC and BC classes, then from C i
have methods to eg. count the number of cars in A and edit one bus in
the middle of the bus array in BC.

Program D - Array of C (multiple trafic scenarios), D should be able to
loop thru all Traffic Classes in C and get the count of Cars from Class
AC

This would be very simple to do if multiple inherritance were allowed:
class C : AC, BC {}

ClassC[] D = new ClassC[5];

D[0].ACountCars or D[1].BEditBus(5).Passangers = 10

When i try to use interface in class BC i get the following error:
Error 1 Interfaces cannot contain fields

The interface BC can not handle the Array of B objects.

How do you get around this?

Patrick
 
It sounds like A and B should be inheriting from C instead of the other way
around. Then you could treat all cars and buses exactly the same except
where it really matters.
 
Doug H said:
It has single inheritance plus interfaces.
C# basically just copied java.

And Delphi, and Objective-C, and Modula-3, and CLU. I would venture that
multiple inheritance is rarer than single inheritance.

-- Barry
 
Barry Kelly said:
And Delphi, and Objective-C, and Modula-3, and CLU.

And Smalltalk.
I would venture that multiple inheritance is rarer than single
inheritance.

In the world of programming languages. In the real world, however, MI is
quite common. :)

///ark
 
Mark Wilden said:
In the world of programming languages. In the real world, however, MI is
quite common. :)

Are you sure? I would guess that single-celled organisms outnumber more
complex organisms by a substantial margin.

:)

-- Barry
 
Barry Kelly said:
Are you sure? I would guess that single-celled organisms outnumber more
complex organisms by a substantial margin.

My species bias is showing. :)
 
Back
Top