Click here to Skip to main content
15,910,303 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Guys,
I am just giving a basic concept, think it in deep.
Let me proceed right away,
Let's assume the following classes,
C#
public class A
{
    public string PropA { get; set; }
    public void MethodOfA()
    {
        Console.WriteLine("Calling MethodOfA from A");
    }
    public void MethodOfB()
    {
        Console.WriteLine("Calling MethodOfB from A");
    }
}
public class B
{
    public string PropA { get; set; }
    public string PropB { get; set; }
    public void MethodOfA()
    {
        Console.WriteLine("Calling MethodOfA from B");
    }
}

C#
public class C : A, B
{
}

Class C is derived from A and B classes
Now, class A and B both contains a common method MethodOfA() So, when I say
MIDL
C c = new C();
c.MethodOfA();


It calls MethodOfA() from A and then it calls MethodOfA from B sequentially.And the output will be
Calling MethodOfA from A
Calling MethodOfA from B
Now, when I say
MIDL
C c = new C();
c.A.MethodOfA();

It calls method of A only and same for B class.
And the output will be
Calling MethodOfA from A
So, I can call method in a row at one shot or I can call it by class name.
Same thing we can apply for the properties too.
We can set properties at one shot or we can get property at one shot.
We can set property by a class name or we can get property by a class name.
Posted
Updated 15-May-11 2:03am
v2

Well the first issue in what you are proposing is the dual inheritance. You can only inherit a single class and all other inheritances must be interfaces.

And if you need a class calling the method of another class than what you need to do is consider the Fascade Pattern for implementing that.
 
Share this answer
 
Comments
Sanjay J Patolia 13-May-11 13:46pm    
yes you are right but if this would be done in specified manner than what are the applications that will use this concepts. I would like to know the applications where this concept will be used. :)
Thanks You :)
Chris Trelawny-Ross 13-May-11 15:03pm    
You should ask this question in a C++ forum. C++ allows multiple inheritance (if I remember right) so you'll find people who've used multiple inheritance who can give you real-life examples of where it helped them. (Or made their lives a nightmare!!)

More usefully, instead of inquiring about applications where the impossible (multiple inheritance in C#) could be used, I would spend some time studying how to use interfaces, and when to use an abstract base class vs. using an interface, for example.
Sanjay J Patolia 13-May-11 22:24pm    
Yes you are right but why to use interfaces if we can do above concepts in C#
Chris Trelawny-Ross 16-May-11 12:11pm    
I recommend that you read about interfaces. They are a way of doing things and are available in multiple languages, including C#. Please note that you cannot, in fact, do "the above concepts" (specifically, multiple inheritance) in C#. So interfaces are the only way you can inherit multiple 'things' in C#.

Using interfaces you can inherit a contract - an agreement on the available properties, methods, etc., that the inheriting class must exhibit. Using class inheritance you inherit not only the contract, but also an implementation of those methods.

C# does not allow you to inherit from more than one base class; to inherit multiple contracts (sometimes known as signatures) you must use interfaces to declare the additional signatures, and then implement their behavior in the implementing class.
#realJSOP 13-May-11 14:05pm    
They've named a pattern for it? Jeeze!
your class C cannot inherit from both parent classes - C# supports only <bold>single inheritance... class C can inherit just from one class: A or B, not both. Another thing with interfaces:
class can implement multiple interfaces
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-May-11 20:08pm    
Hm, correct, a 5.
--SA
Sanjay J Patolia 13-May-11 22:18pm    
Yes you are right but I am talking about a case where it is possible here. Just believe it for once and than then think of applications where we can use above concepts.
Sanjay J Patolia 13-May-11 22:18pm    
Yes you are right but I am talking about a case where it is possible here. Just believe it for once and than then think of applications where we can use above concepts.
Chris Trelawny-Ross 16-May-11 12:15pm    
A C# Question & Answer topic is the wrong place to ask hypothetical questions such as the one you're asking. In C# multiple inheritance simply is not possible - so asking us to "believe it for once" is not going to get much of a response in this section.

In this forum we're only interested in discussing and/or learning about what IS possible in C#, not about things that aren't.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900