hello sir/madam,
could please tell me why multiple inheritance not possible in .net..
hello sir/madam,
could please tell me why multiple inheritance not possible in .net..
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Joel CochranPosted Jan 4, 2008, 1:42 PM
Let's use a classic example: Employee. Not all employees are created equally: some are hourly, some are salaried. Some are part time, some are full time. Many of these examples create subclasses like HourlyEmployee, SalariedEmployee, etc. The implementation details for calculating their pay could be vastly different, so you can't define the method in the parent Employee class. So via an Interface, like IPayrollEmployee, you require that there exists a CalculatePay method, with a specific signature (like pay period date range), but you leave the implementation details up to the implementing class.
Now, you can create a Collection of IPayrollEmployee objects, and anything that implements IPayrollEmployee can be in that Collection. And each object, whether it is SalariedEmployee or HourlyEmployee will have a CalculatePay method with a consistent signature even though the details are different.
So Interfaces are good for building objects with consistent member signatures. They are also handy for grouping and processing like objects. Ultimately, Interfaces define behavior. Everything that implements IDisposable can be used in a like manner, but not every object disposes of itself in the same way. Some objects need to close FileStreams, some need to clean up database connections, some need to do nothing. The point is that with Interfaces, it is up to the developer to define how something is done.
This is very powerful, and brings us full circle. It is correct that implementing multiple Interfaces is no substitution for real multiple inheritance (which would be a nightmare). But it is a great mechanism for defining (not inheriting) multiple sets of behavior. So I can create a custom sequence (IEnumerable
I'll give you one more example and then I'll quit. One of my first .NET projects was to create a single point of entry for multiple ADO.NET managed providers. I created an Interface called IDBAccess that defined all the signatures I needed, like RunSelectStatement, ExecuteNonQuery, ExecuteScalar, etc. I then created wrapper classes for each of the different ADO.NET providers I wanted to support. All of these implemented IDBAccess, but for each one they require different references to different namespaces. They all used different classes for ADO access. They all have different Connection String formats. The list goes on about how they are different, but the magic here is how they are the same: they all need to expose the same behaviors, as described in IDBAccess. To finish it all up, I created a master DBAccessManager class that also implemented IDBAccess, but all it did was manage an appropriate IDBAccess instance. The result is that I have a single, unified set of methods for working with Databases without worrying about all the snarky details.
I hope this helps: Interfaces are your friend!
sbugigPosted Jan 4, 2008, 8:34 AM
Can any one then tell me why do we really need interfaces when we still need to implement them on every class that implements it.
why cant you just define your methods and fiels on your class and use them, what defference does it make.
i really dont understand this interface thing.
Posted Jan 3, 2008, 4:57 PM
Therefore the class is not inheriting anything really, since an interface, unlike a class, cannot create method or property bodies, but merely their prototypes.
In true inheritance, a class can inherit predefined methods, properties, fields, etc... from a class that they were defined in and use those predefined members without having to define them itself, unless of course those members are virtual and you want to override them.
The only really good use I have found for interfaces is that if a class member is accessing another classes members, it can check to see if that class implements a specified interface first before attempting to call its methods. This is useful for conformity in objects.
Still, interfaces are not a true replacement for multiple inheritance, not that Ive found anyway :)
Raj KumarPosted Dec 18, 2007, 11:32 PM
kavita_andePosted Dec 5, 2007, 11:01 AM
The reason for this is confliction which occurs whenever function with same name appears in both classes. Ex: If there is a function x() in both the base classes then for compiler it will be difficult to decide which function is to use in derived class.While in interfaces functions are declared in interface and defined in derived class.