Skip to content

C# Inheritance – programs for practice

Q) What is output of below C# code?

class Car {
		
	public void run(){
	
		Console.WriteLine("Car is running");
	}
	
}

class Maruti : Car {
	
	public void run(){
		
		Console.WriteLine("Maruti Car is running");
	}
	
}

class Program
{
    static void Main(string[] args)
    {
        Car c = new Maruti();
        c.run();
    }
}

Output: Car is running

Explanation: In fact, this is name hiding feature in C# language. If base class and derived class have same method name and have not used virtual and override in base and derived class respectively then on creating object of derived class and assigning to base reference will call base class method. Recommended to read name hiding feature in C# with example


Q) What is output of C# program example?

class X
{
    
    public virtual void foo()
    {
        Console.WriteLine("Parent");
    }

}

class Y : X
{
    public override void foo()
    {
        Console.WriteLine("Child");
    }

}

class Program
{
    static void Main(string[] args)
    {
        X c = new Y();
        c.foo();
    }
}

Output: Child

Explanation: In this C# code example, method overriding feature has been implemented. For overriding, the method should be virtual in base class and the same method in derived class preceding by override keyword. Read in detail about method overriding in C# language


Q) What is output of C# code example?

class Php
{
    public void PhpProgrammer()
    {
        Console.WriteLine("php programmers");
    }

}
class CSharp
{   

    public void CSharpProgrammer()
    {
        Console.WriteLine("CSharp Programmers");
    }

}
class Programmers : Php, CSharp
{
    
}

class Program
{
    static void Main(string[] args)
    {
        Programmers p = new Programmers();
        p.PhpProgrammer();
        p.CSharpProgrammer();        
    }
}

Output: compiler error

Explanation: C# language does not support multiple inheritance. In this C# code example we are trying to inherit two classes that is wrong.
However, we can inherit multiple interfaces.Read the program example of C# multiple inheritance using interface.


Q) What is output of following C# code?

class Car
{
    int cc = 1000;

    public void display()
    {
        Console.WriteLine("Value :"+this.cc);
    }

}

class Maruti : Car
{
    int cc = 2000;

    public void display()
    {
        Console.WriteLine("Value :" + this.cc);
    }

}

class Program
{
    static void Main(string[] args)
    {
        Car c = new Maruti();
        c.display();
    }
}

Output: Value :1000

Explanation:

If we make base class function virtual and override in derived class then output will be from derived class like below C# code. i.e. Value :2000

class Car
{
    int cc = 1000;

    public virtual void display()
    {
        Console.WriteLine("Value :"+this.cc);
    }

}

class Maruti : Car
{
    int cc = 2000;

    public override void display()
    {
        Console.WriteLine("Value :" + this.cc);
    }

}

class Program
{
    static void Main(string[] args)
    {
        Car c = new Maruti();
        c.display();
    }
}

Pages: 1 2 3


Published inC# program exercises

2 Comments

  1. Thanks Pradnya for pointing it out. Corrected!

  2. Pradnya

    Here in bird and parrot example comment “if we use derived object in base reference then base class method will be called e.g. Bird b = new Parrot();: ” is wrong. It should be “if we use derived object in base reference then derived class method will be called” as we have used virtual and override used in example.

Comments are closed.