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.