Advertisement
class derived-class-name : base-class-name
{
//members of derived class
}
//C# Example of Inheritance
using System;
class A
{
//An instance variable with public access
public int a = 10;
//An instance variable with protected access
protected int b = 20;
//A method with public access
public void GetName()
{
Console.WriteLine("A");
}
}
// Class B inheriting class A
class B:A
{
public static void Main(String[] ar)
{
//Creating an object of class B
B ob = new B();
//Calling the inherited GetName() method
//by using the object of B class
ob.GetName();
//Accessing the inherited instance variable, a
//by using the object of B class
Console.WriteLine(ob.a);
//Accessing the inherited instance variable, b
//by using the object of B class
Console.WriteLine(ob.b);
}
}
A
10
20
//C# Private member are never inherited
using System;
class A
{
//Instance variable with private access
private int a = 10;
//A method with private access
private void GetName()
{
Console.WriteLine("A");
}
}
// Class B inheriting class A
class B:A
{
public static void Main(String[] ar)
{
//Creating an object of class B
B ob= new B();
//Calling the not-inherited GetName() method
//by using the object of B class
//This will issue a compile error
ob.GetName();
//Accessing the not-inherited instance variable, a
//by using the object of B class
//This will also issue a compile error
Console.WriteLine(ob.a);
}
}
A.cs(28,5): error CS0122: 'A.GetName()' is inaccessible due to its protection level
A.cs(33,23): error CS0122: 'A.a' is inaccessible due to its protection level
Advertisement
//C# Example of single level inheritance i.e. when one class inherits another class
using System;
class A
{
public void ShowA()
{
Console.WriteLine("A");
}
}
//class B inheriting features of class A
class B:A
{
public void ShowB()
{
Console.WriteLine("B");
}
public static void Main(String[] ar)
{
//Creating an object of class B
B ob = new B();
//Calling the inherited method of class A
//by using the object of class B
ob.ShowA();
//Calling the method of class B
//by using the object of class B
ob.ShowB();
}
}
//C# Example of Multi-level inheritance
using System;
class A
{
public void ShowA()
{
Console.WriteLine("A");
}
}
//class B inheriting the features of class A
class B:A
{
public void ShowB()
{
Console.WriteLine("B");
}
}
//class C inheriting the features of class B
class C:B
{
public void ShowC()
{
Console.WriteLine("C");
}
public static void Main(String[] ar)
{
//Creating an object of class C
C ob = new C();
//Using the object of class C
//to access the inherited method of class A
ob.ShowA();
//Using the object of class C
//to access the inherited method of class B
ob.ShowB();
//Using the object of class C
//to access method of its own class
ob.ShowC();
}
}
A
B
C
//C# A direct multiple inheritance is not allowed
//which means, you cannot inherit multiple classes, all at once.
using System;
class A
{
public void ShowA()
{
Console.WriteLine("A");
}
}
//class B
class B
{
public void ShowB()
{
Console.WriteLine("B");
}
}
//class C tring to inherit the features of class A and B at once
//This will issue a compile error
class C:B,A
{
public void ShowC()
{
Console.WriteLine("C");
}
}
A.cs(27,11): error CS1721: Class 'C' cannot have multiple base classes: 'B' and 'A'
//C# Example of creating a specialized class by inheriting general class
using System;
//A general class, Fruit
class Fruit
{
public void ShowFruit()
{
Console.WriteLine("This is a fruit");
}
}
//A specialized class, Mango, which is inheriting class Fruit
class Mango:Fruit
{
public void FruitColor()
{
Console.WriteLine("Color - Yellow");
}
public void FruitName()
{
Console.WriteLine("Name - Mango");
}
public void HasSeed()
{
Console.WriteLine("Seed - Yes");
}
}
class A
{
public static void Main(String[] ar)
{
//Creating an object of Mango class
Mango ob= new Mango();
//Accessing the inherited "general" method ShowFruit() of Fruit class
//by using the object of Mango class
ob.ShowFruit();
//Using the object of Mango class
//To call its specialized methods
ob.FruitName();
ob.FruitColor();
ob.HasSeed();
}
}
This is a fruit
Name - Mango
Color - Yellow
Seed - Yes
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement