Pages

Sunday, May 12, 2013

inheritance in c#

inheritance:

By using inheritance we can achive code reusability 
->Better mantenability
->Avoid duplication
-> Multiple inheritance not applicable in inheritance(for avoiding this we  are use interfaces)
Types:
1.single
2.multiple
3.multilevel
4.hybrid

 sample code:

public class test
{
protected void page_load(-,)
{
  B b=new B();
   b.m();    // without create object we can access methods and variable(only possable inheritance)
  b.m1();
public class A
 {
  public int x=20;
 public int m()
    {
      return 200;
   }
public class B:A
  {
    public int y=30;
   public string m1()
    {
      return "mother";
   }
}

Q).why multiple inheritance of classes in not supported in C#?

ans:
There is no definitive answer to this question. It is a matter of taste.
There is the classic diamond problem encountered in multiple inheritance, in which class D inherits from both B and C, which both inherit from A.
  A
 / \
B   C
 \ /
  D
So which copy of A does D get? The one from B, the one from C? Both? This way various languages resolve this problems is discussed here:

No comments:

Post a Comment