Inheritance can be classified to 5 types.
- Single Inheritance
- Hierarchical Inheritance
- Multi Level Inheritance
- Hybrid Inheritance
- Multiple Inheritance
when a single derived class is created from a single base class then the inheritance is called as single inheritance.
2. Hierarchical Inheritance
when more than one derived class are created from a single base class, then that inheritance is called as hierarchical inheritance.
3. Multi Level Inheritance
when a derived class is created from another derived class, then that inheritance is called as multi level inheritance.
4. Hybrid Inheritance
Any combination of single, hierarchical and multi level inheritances is called as hybrid inheritance.
5. Multiple Inheritance
when a derived class is created from more than one base class then that inheritance is called as multiple inheritance. But multiple inheritance is not supported by .net using classes and can be done using interfaces.
Classes can inherit from another
class. This is accomplished by putting a colon after the class name when
declaring the class, and naming the class to inherit from—the base class—after
the colon, as follows:
C#
public class A
{
public A() { }
}
public class B : A
{
public B() { }
}
The new class—the derived class—then gains all the
non-private data and behavior of the base class in addition to any other data
or behaviors it defines for itself. The new class then has two effective types:
the type of the new class and the type of the class it inherits.
Example:
class Program
{
public static void Main()
{
Country
objcountry = new Country();
objcountry.ToString();
Console.WriteLine("\nThe derived class:");
Country
objcity = new City();
objcity.ToString();
Console.ReadLine();
}
}
class Country
{
public
Country()
{
Console.WriteLine("This is India");
}
}
class City : Country
{
public
City()
{
Console.WriteLine("Delhi is capital of India");
}
}
Output:
This is India
The derived class:
This is India
Delhi is capital of India
Example 2:
class Program
{
public static void Main()
{
Country
objcountry = new Country();
objcountry.ToString();
Console.WriteLine("\nThe derived class:");
Country
objcity = new City();
objcity.ToString();
objcity.India();
Console.ReadLine();
}
}
class Country
{
public
Country()
{
Console.WriteLine("This is Country class");
}
public void India()
{
Console.WriteLine("This is India");
}
}
class City : Country
{
public
City()
{
Console.WriteLine("This is City class");
}
}
Output:
This is Country class
The derived class:
This is Country class
This is City class
This is India
Example 3:
class Program
{
public static void Main()
{
Country
objcountry = new Country();
objcountry.ToString();
Console.WriteLine("\nThe derived class:");
Country
objcity = new City();
objcity.ToString();
objcity.India();
Console.ReadLine();
}
}
class Country
{
public
Country()
{
Console.WriteLine("This is Country class");
}
public void India()
{
Console.WriteLine("This is India");
}
}
class City : Country
{
public
City()
{
Console.WriteLine("This is City class");
}
public new void India()
{
Console.WriteLine("Delhi is capital of India");
}
}
Output:
This is Country class
The derived class:
This is Country class
This is City class
No comments:
Post a Comment