-2

I have the below code in my Application.

public class GeneralInfo
        {
            private string _id;
            private string _name;           

            public string id
            {
                set
                {
                    _id = value;
                }
                get
                {
                    return _id;
                }
            }

            public string name
            {
                set
                {
                    _name = value;
                }
                get
                {
                    return _name;
                }
            }

        }

       public class SecureInfo
       {
           private string _password;

           public string password
           {
               set
               {
                   _password = value;
               }
               get
               {
                   return _password;
               }
           }

       }


public class User
{
}

I need to apply multiple inheritance in the above code ie. the classes GeneralInfo,SecureInfo properties should be accessible in the user class.

I know using interface Multiple inheritance can be achieved. But i need to define the properties in the base class which is restricted in Interface.

How I can achieve this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Tom Cruise
  • 1,395
  • 11
  • 30
  • 58
  • Not. There's no multiple inheritance in C#. If interfaces don't do the trick for you, then you're out of options. – GolezTrol Jul 04 '13 at 13:00
  • 1
    Are you sure you need inheritance ? You say "the classes GeneralInfo,SecureInfo properties should be accessible in the user class". So maybe GeneralInfo and SecureInfo should simply be properties of the user class... – Raphaël Althaus Jul 04 '13 at 13:04
  • 2
    Are you sure you need it? Looks like a User _is a_ `GeneralInfo` but I think it _has a_ `SecureInfo`. – H H Jul 04 '13 at 13:04
  • @Bogdan M Where I missed? – Tom Cruise Jul 04 '13 at 13:05
  • @HenkHolterman Yes. I need to access both the classes in User class. – Tom Cruise Jul 04 '13 at 13:06
  • 5
    Composition over Inheritance. A User is not a SecureInfo. This is what inheritance mean. A User has SecureInfo. See http://en.wikipedia.org/wiki/Composition_over_inheritance – Mirco Jul 04 '13 at 13:06
  • @user833985 - that is not an answer to the question. And that's where you miss the point. – H H Jul 04 '13 at 13:06
  • 1
    @RaphaëlAlthaus because these classes will be reused among many other classes like User. For eg: GeneralInfo will be common among the classes User,books,sites which can be reusuable. – Tom Cruise Jul 04 '13 at 13:14

7 Answers7

5

C# does not support multiple inheritance. However you can achieve this via multiple interfaces.

public interface ISecureInfo 
{

}

public interface IGeneralInfo 
{

} 

public class UserClass : ISecureInfo, IGeneralInfo {

}
Darren
  • 68,902
  • 24
  • 138
  • 144
1

You probably better off encapsulating the data in the class rather than trying to use something to do multiple inheritance here. See this question for some arguments for this.

Community
  • 1
  • 1
Matthew Steeples
  • 7,858
  • 4
  • 34
  • 49
1

You can achieve this through interface based inheritance:

public interface IGeneralInfo 
{
    String Id { get; set; }
    String Name { get; set; }
}

public interface ISecureInfo
    String Password { get; set; }
}


public class User : IGeneralInfo, ISecureInfo
{
    // Implementation of IGeneralInfo
    public String Id { get; set; }
    public String Name { get; set; }

    // Implementation of ISecureInfo
    public String Password { get; set; }
}

Or, going one step further, through composition:

public interface IGeneralInfo 
{
    String Id { get; set; }
    String Name { get; set; }
}

public class GeneralInfo : IGeneralInfo
{
    public String Id { get; set; }
    public String Name { get; set; }
}

public interface ISecureInfo
    String Password { get; set; }
}

public class SecureInfo : IGeneralInfo
{
    public String Password { get; set; }
}

public class User : IGeneralInfo, ISecureInfo
{
    private GeneralInfo generalInfo = new GeneralInfo();
    private SecureInfo secureInfo = new SecureInfo();

    public String Id { 
        get { return generalInfo.Id; }
        set { generalInfo.Id = value; } 
    }
    public String Name { 
        get { return generalInfo.Name; }
        set { generalInfo.Name = value; } 
    }

    public String Password { 
        get { return secureInfo.Password; }
        set { secureInfo.Password = value; } 
    }
}
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
0

You cannot do multiple inheritance in C# because it is not supported like C++. In C# you can use interfaces for it and implement method and properties. For sample, you could have a base class

public abstract class Entity
{
    public string Name { get; set; }

}

You also could have some interfaces:

public interface IPrint
{
   void Print();
}

public interface IGenerate
{
   void Generate();
}

And use it like multiples inheritance (but it is not, it is just a single inheritance and interfaces)

public class User : Entity, IPrint, IGenerate
{
   public void Print()
   {
     // some code
     // here you could access Name property, because it is on base class Entity
   } 

   public void Generate()
   {
     // some code
   } 
}

And you could instance it using the abstractions:

Entity e = new User();
IPrint p = new User();
IGenerate g = new User();
User u = new User();

If you need implementations, you could do a hiearachy inherits, for sample:

User inherit from Person that inherit from Entity.

public class Entity
{
   public int Id { get; set; }


   public void Method()
   {
      // some code
   }
}

public class Person : Entity
{
   public string Name { get; set; }

   public void AnotherMethod()
   {
      // some code
   }
}

public class User : Person
{
   public string Password { get; set; }

   public bool CheckUser(string name, string passworkd)
   {
      // some code
   }
}
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
0

From your sample description, encapsulation might be what you might want to use:

public class Info{
    GeneralInfo general;
    SecureInfo secure;
...
}
Brad Bruce
  • 7,638
  • 3
  • 39
  • 60
0

I think the best would be to seperate the implementation of the interfaces and the real class you have at the end. What I mean is something like the Bridge Pattern. Your class (that will implement several interfaces) will just deleagte the method calls to the real implementation, that you can have in a seperate place and only once.

Tintenfiisch
  • 360
  • 5
  • 19
0

You could also use an approach like this. You would get to the same point than if you would be using multiple inheritance. That way, you could inherit only Entity if you don't need the SecureInfo stuff (i.e. for books and other stuff). Still, I think composition would do better in this case as others say...

class User : SecuredEntity { }

abstract class SecuredEntity : Entity, ISecureInfo
{
    public string Password { get; set; }
}

abstract class Entity : IGeneralInfo
{
    public string ID { get; set; }
    public string Name { get; set; }
}

interface IGeneralInfo
{
    string ID { get; set; }
    string Name { get; set; }
}

interface ISecureInfo
{
    string Password { get; set; }
}
maxim1500
  • 118
  • 1
  • 12