Inheritance in C#
Inheritance is one of the most important feature of the object oriented Programming Language. In Inheritance we can reuse the properties of one class into another class by extending the class. The main idea behind Inheritance is the re usability of the programming code.
There are two Concepts in Inheritance. Base Class and Derived class. Base class is the class from which the properties are going to be inherited. Derived class is the class in which properties are inherited.
- Single Inheritance In single Inheritance , There is one base class and one derived class. The properties and methods are in base class and base class is extended to derived class. For better understanding , see the below image.
- Hirarchical inheritance In this type of inheritance ,multiple classes are inherited using a single base class.The idea behind this is to use the features of a single class into multiple derived classes.
- Multilevel Inheritance In this inheritance ,The class is derived from another derived class. This type of inheritance is used when we want to use some properties of our predecessor class not the base class like hirarchical inheritance.
- Multiple Inheritance(Using Interface) C# does not support multiple Inheritance just like other object oriented languages. To implement multiple inheritance , c# introduced concept of Interface. Using Interface, we can implement multiple inheritance.
using System;
namespace ConsoleApplication2
{
public class Base
{
public void HelloBase()
{
Console.WriteLine("Base Hello");
}
}
public class Derived : Base
{
public void HelloDerived()
{
HelloBase();
Console.WriteLine("Derived Hello");
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
Base b = new Base();
Derived d = new Derived();
d.HelloBase();
d.HelloDerived();
}
}
}
namespace ConsoleApplication2
{
public class Base
{
public void HelloBase()
{
Console.WriteLine("Base Hello");
}
}
public class Derived : Base
{
public void HelloDerived()
{
HelloBase();
Console.WriteLine("Derived Hello");
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
Base b = new Base();
Derived d = new Derived();
d.HelloBase();
d.HelloDerived();
}
}
}
Output
Base Hello
Base Hello
Derived Hello
Base Hello
Derived Hello
using System;
namespace hirarchical
{
public class Base
{
public void message()
{
Console.WriteLine("Base Hello");
}
}
public class Derived:Base
{
public void DerivedMessage()
{
message();
Console.WriteLine("Derived Hello");
}
}
public class B : Base
{
public void BMessage()
{
message();
Console.WriteLine("3rd derived from Base");
}
}
class Program
{
static void Main(string[] args)
{
Base b = new Base();
Derived d = new Derived();
B bob=new B();
bob.BMessage();
Console.ReadKey();
}
}
}
namespace hirarchical
{
public class Base
{
public void message()
{
Console.WriteLine("Base Hello");
}
}
public class Derived:Base
{
public void DerivedMessage()
{
message();
Console.WriteLine("Derived Hello");
}
}
public class B : Base
{
public void BMessage()
{
message();
Console.WriteLine("3rd derived from Base");
}
}
class Program
{
static void Main(string[] args)
{
Base b = new Base();
Derived d = new Derived();
B bob=new B();
bob.BMessage();
Console.ReadKey();
}
}
}
Output
Base Hello
3rd Derived from Base
3rd Derived from Base
using System;
namespace Multilevel
{
public class Base
{
public void Say()
{
Console.WriteLine("Base Hello");
}
}
public class Derived : Base
{
public void DerivedSay()
{
Say();
Console.WriteLine("Derived Hello");
}
}
public class B : Derived
{
public void Speak()
{
DerivedSay();
Console.WriteLine("B class Hello");
}
}
class Program
{
static void Main(string[] args)
{
B b = new B();
b.Speak();
Console.ReadLine();
}
}
}
namespace Multilevel
{
public class Base
{
public void Say()
{
Console.WriteLine("Base Hello");
}
}
public class Derived : Base
{
public void DerivedSay()
{
Say();
Console.WriteLine("Derived Hello");
}
}
public class B : Derived
{
public void Speak()
{
DerivedSay();
Console.WriteLine("B class Hello");
}
}
class Program
{
static void Main(string[] args)
{
B b = new B();
b.Speak();
Console.ReadLine();
}
}
}
Output
Base Hello
Derived Hello
B class Hello
Derived Hello
B class Hello
using System;
namespace interfaceexample
{
public interface i
{
string show();
}
public class A:i
{
public string show()
{
return "language expert";
}
}
}
namespace interfaceexample
{
public interface i
{
string show();
}
public class A:i
{
public string show()
{
return "language expert";
}
}
}
Inheritance in C#
Reviewed by LanguageExpert
on
March 02, 2018
Rating:
No comments