Playing with inheritance in C#.NET

Prabhat39
Posted by in C# category on for Beginner level | Points: 250 | Views : 3581 red flag

This article explains the different behavior of inheritance in different cases.

Introduction
 
This article explains the different behavior of inheritance in different cases. Inheritance is the ability to create classes which inherits certain aspects from parent classes.
 
Objective
 
To understand inheritance and its behavior in different cases. To understand keywords like virtual, override, new and abstract.
 
CASE 1: What will happen when virtual keyword used with a method without implementation?
CODE: 

class A

{

   public virtual void Show();

} 

RESULT:
Error      1              'ConsoleApplication.A.Show()' must declare a body because it is not marked abstract, extern, or partial               
 
CASE 2: What will happen when a method used without implementation.
CODE:

class A

{

   public void Show();

}

RESULT:
Error      1              'ConsoleApplication.A.Show()' must declare a body because it is not marked abstract, extern, or partial               
 
CASE 3: What will be the output of the C#.NET code snippet given below? When base class method overridden by derived class using override keyword?
CODE:

    class Program

    {

        static void Main(string[] args)

        {

            A obj = new B();

            obj.Show();

            Console.ReadLine();

        }

    }

    class A

    {

        public virtual void Show()

        {

            Console.WriteLine("A.Show()");

        }

    }

    class B : A

    {

        public override void Show()

        {

            Console.WriteLine("B.Show()");

        }

    }

RESULT:
B.Show()
 
CASE 4: What will be the output of the C#.NET code snippet given below?  When base class method overridden by derived class using new keyword?
CODE:

  class Program

    {

        static void Main(string[] args)

        {

            A obj = new B();

            obj.Show();

            Console.ReadLine();

        }

    }

    class A

    {

        public virtual void Show()

        {

            Console.WriteLine("A.Show()");

        }

    }

    class B : A

    {

        public new void Show()

        {

            Console.WriteLine("B.Show()");

        }

    }

RESULT:
A.Show()
 
CASE 5: What will be the output of the C#.NET code snippet given below?
CODE:

  class Program

    {

        static void Main(string[] args)

        {

            A obj = new B();

            obj.Show();

            Console.ReadLine();

        }

    }

    class A

    {

        public virtual void Show()

        {

            Console.WriteLine("A.Show()");

        }

    }

    class B : A

    {

        public void Show()

        {

            Console.WriteLine("B.Show()");

        }

    }

RESULT:
Warning               1              'ConsoleApplication.B.Show()' hides inherited member 'ConsoleApplication.A.Show()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Output:
A.Show()
 
CASE 6: What happen when abstract method is used with non-abstract class?
CODE:

    class A

    {

        public abstract void Show()

        {

            Console.WriteLine("A.Show()");

        }

    }

RESULT:
Error      1              'ConsoleApplication.A.Show()' cannot declare a body because it is marked abstract      
Error      2              'ConsoleApplication.A.Show()' is abstract but it is contained in non-abstract class 'ConsoleApplication.A'               
 
 
CASE 7: What will be the output of the C#.NET code snippet given below?
CODE:  

    class Program

    {

        static void Main(string[] args)

        {

            C c = new C();

            A a = new A();

            a = c;

            a.Show();

            c.Show();

            Console.ReadLine();

        }

    }

    class A

    {

        public virtual void Show()

        {

            Console.WriteLine("A.Show()");

        }     

       

    }

    class B : A

    {

        public new void Show()

        {

            Console.WriteLine("B.Show()");

        }

    }

    class C : B

    {

        public new void Show()

        {

            Console.WriteLine("C.Show()");

        }

    }

RESULT:
A.Show()
C.Show()
 
CASE 8: What will be the output of the C#.NET code snippet given below?
CODE: 

    class Program

    {

        static void Main(string[] args)

        {

            C c = new C();

            A a = new A();

            a = c;

            a.Show();

            c.Show();

            Console.ReadLine();

        }

    }

    class A

    {

        public virtual void Show()

        {

            Console.WriteLine("A.Show()");

        }     

       

    }

    class B : A

    {

        public override void Show()

        {

            Console.WriteLine("B.Show()");

        }

    }

    class C : B

    {

        public new void Show()

        {

            Console.WriteLine("C.Show()");

        }

    }

RESULT:
B.Show()
C.Show()
 
CASE 9: What will be the output of the C#.NET code snippet given below?
CODE: 

    class Program

    {

        static void Main(string[] args)

        {

            C c = new C();

            A a = new A();

            a = c;

            a.Show();

            c.Show();

            Console.ReadLine();

        }

    }

    class A

    {

        public virtual void Show()

        {

            Console.WriteLine("A.Show()");

        }     

       

    }

    class B : A

    {

        public override void Show()

        {

            Console.WriteLine("B.Show()");

        }

    }

    class C : B

    {

        public override void Show()

        {

            Console.WriteLine("C.Show()");

        }

    }

RESULT:
C.Show()
C.Show() 
  
CASE 10: What will be the output of the C#.NET code snippet given below?
CODE: 

    class Program

    {

        static void Main(string[] args)

        {

            B b = new B(10);

            Console.ReadLine();

        }

    }

    class A

    {

        int i;

        public A(int j)

        {

            i = j;

            Console.WriteLine("Base");

        }

    }

    class B : A

    {

        public B(int j)

        {

            Console.WriteLine("Derived");

        }

    }

RESULT:
Error      1              'ConsoleApplication.A' does not contain a constructor that takes 0 arguments  
 
CASE 11: What will be the output of the C#.NET code snippet given below?
CODE: 

  class Program

    {

        static void Main(string[] args)

        {

            B b = new B(10);

            Console.ReadLine();

        }

    }

    class A

    {

        int i;

        public A(int j)

        {

            i = j;

            Console.WriteLine("Base");

        }

    }

    class B : A

    {

        public B(int j)

            : base(j)

        {

            Console.WriteLine("Derived");

        }

    }

RESULT:
Base
Derived
 
Conclusion
Inheritance provides great reusability of the code.

Page copy protected against web site content infringement by Copyscape

About the Author

Prabhat39
Full Name: Prabhat Kumar
Member Level:
Member Status: Member
Member Since: 12/21/2012 5:30:10 AM
Country: India
Regards, Prabhat Kumar
http://www.dotnetfunda.com
Prabhat is a programmer and having 5+ years of developing experience in IT field. Major strength are C#, WPF, asp.net and SQL server 2008. He is also having good exposure in WCF.

Login to vote for this post.

Comments or Responses

Posted by: Sheonarayan on: 1/14/2013 | Points: 25
Good article Prabhat, keep exploring and sharing.
Posted by: Prabhat39 on: 1/14/2013 | Points: 25
Thanks
Posted by: Niranjan44 on: 1/14/2013 | Points: 25
Posted by: Kundan64 on: 1/16/2013 | Points: 25
Very good article!

Login to post response

Comment using Facebook(Author doesn't get notification)