Introducation to Inheritance in C#


In this article I am going to explain you in detail about the Inheritance in C#. It may be very helpful for new user. Inheritance is most useful scope of object oriented programming systems. With the help of this article user can easily use inheritance in C# ( framework) .Inheritance similar like polymorphism and other feature of OOPS.

As inheritance is used when a class is build upon another class .In the term of data or behavior and it adheres to the rules of substitutability namely, that the derived class can be substituted for the base class .
An example of this would be if you were writing a hierarchy of application class you want to have a class for handing member A application member B application. Assume these applications differ in some respect you had wanted to a class for each application. However both application dp share enough functionality that you would want to put the common functionality into a base class, derive the other two classes from the base class , and override or modify the inherited base class behavior at times.
To the inherit one class form another, you would use the following syntax:

Class derived class:base class


Here is what this application example would look like:


Using system;
Class application
{
Public application()
{
commonField=100;
}
Public int commonfield;
Public void commonmethod()
{
Console.WriteLine("application.Common Method");
}
}
Class MemeberA : application
{
Public void SomeMethodSpecificToMemberA()
{
Console.WriteLine("MemberA. SomeMethodSpecificToMemberA")
}
}
Class MemeberB : application
{
Public void SomeMethodSpecificToMemberB()
{
Console.WriteLine("MemberB. SomeMethodSpecificToMemberB")
}
}
Class InheritanceApp
{
Public static void main()
{
MemberA MemberA= new MemberA();

MemberA. SomeMethodSpecificToMemberA();
MemberA.CommonMethod();
Console.WriteLine("Inherited common field={0}",memberA.CommonField);
}
}

Compiling and excuting this appilication result in the following output"
MemberA. SomeMethodSpecificToMemberA
Application. Common Method
Inherited common field = 42
Example of Inheritance

class App
{
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()


Notice:
Application. Common Method and Application. Common Field method are now a part of MemebrA class definition. This is because, the member A and memberB classes are derived from the base Application class , they are both inherit almost all of its members that are defined as public protected or internal.


Comments

Author: koti Balaji22 Jul 2013 Member Level: Silver   Points : 10

Multiple-Inheritance not possible in c#
so Using Interface concept

Class A
{}
Class B
{ }
Class C: A, B // not possible
{ }

// so Multiple inheritence using 2 interface

*************** example ************

using System;
using System.Collections.Generic;
namespace ConsoleApplication37
{
interface A // act as base class1
{ // interface act as abstract class
//because ,No method body ,cant create obj
void Method1();

}
interface B // act as base class2
{
void Method2();
}
class C : A, B
{
//derived class
public void Method1()
{
Console.WriteLine("Method1 () called");
}
public void Method2()
{
Console.WriteLine("Method 2 () called");
}
}
class Program
{
static void Main(string[] args)
{
C obj = new C();
obj. Method1();
obj. Method2();
}
}}
OUTPUT
Method1 () called
Method 2 () called



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: