Single Inheritance – C Sharp example

Single inheritance C# example – Single inheritance is just a parent and child class relationship. There should be one base class and one child class.

Existing class is known a as base class or parent class. The class that access the features i.e. fields and methods is known as subclass, derived class or child class.

Child class use “:” Operator to inherit feature of parent class e.g. class Son : Father{}

Let’s consider and example,

A son wants to use his father’s home and car and he has his own mobile only. So, the son can access his father’s home and car.

So, Father is the parent class and Son is the child class.

Single inheritance C# example – Source Code:

 //base / parent class
 //base / parent class
class Father
{
	
	public void home()
        {
		Console.WriteLine("Father's home");
	}
	
	public void Car()
        {
		Console.WriteLine("Father's Car");
	}
}
 
//Inherit /derived / extends
class Son : Father 
{
	
	public void mobile()
        {
        Console.WriteLine("Son's mobile");
	}
}




public class CSharpinheritance
{

    public static void Main(String[] args)
    {

        Son s = new Son();
        s.mobile();
        s.Car();
        s.home();

    }

}

Output:

Father’s Home
Father’s Car
Son’s mobile

Related Posts