C# Inheritance – programs for practice


Q) What is the output of C# code example?

class C1
{   
    public void printC1(){
       
        Console.WriteLine("C1");
    }    
}
 
class C2:C1
{
    public void printC2(){
       
        Console.WriteLine("C2");
    }    
}
 
class C3:C2
{
    public void printC3()
    {
        Console.WriteLine("C3");
    }    
}

class Program
{
    static void Main(string[] args)
    {
        C3 obj = new C3();
        obj.printC1();
        obj.printC2();
        obj.printC3();        
    }
}

Output:
C1
C2
C3
Explanation: This C# code example shows multi-level inheritance. C3 class can access both C1 and C2 public methods and properties. Also, note that C2 class can only access class C1 public methods and does not know anything about C3.


Q) What is output of below C# code?

class Bird
{   
    public void fly(){
       
        Console.WriteLine("Bird can fly");
    }    
}

class Parrot : Bird
{
    public void fly()
    {
        Console.WriteLine("Parrot can fly");
    }
} 

class Program
{
    static void Main(string[] args)
    {
        Bird b = new Bird();
        b.fly();
        Bird b1 = new Parrot();
        b1.fly();
    }
}

Output:
Bird can fly
Bird can fly
Explanation:
Bird b1 = new Parrot(); b1.fly(); – Bird base class contains the object of derived class called Parrot.So, it seems like derived method should be called. But,since,the method in derived class has the same name but not overridden. Hence, In this case also, base class method will be called.

If we want to call derived class method using base class reference, then we need to make base class method virtual and override it in derived class. then it will output Parrot can fly. See the below example. Recommended to read method overriding run time polymorphism in c# and name hiding in C# language.

class Bird
{   
    public virtual void fly(){
       
        Console.WriteLine("Bird can fly");
    }    
}

class Parrot : Bird
{
    public override void fly()
    {
        Console.WriteLine("Parrot can fly");
    }
} 

class Program
{
    static void Main(string[] args)
    {
        Bird b = new Bird();
        b.fly();
        Bird b1 = new Parrot();
        b1.fly();
    }
}

Q)What is output of C# code example?

class Bird
{   
    public virtual void fly(){
       
        Console.WriteLine("Bird can fly");
    }    
}

class Parrot : Bird
{
    public override void fly()
    {
        Console.WriteLine("Parrot can fly");
    }
} 

class Program
{
    static void Main(string[] args)
    {
        Parrot p = new Parrot();
        p.fly();
    }
}

Output:
Parrot can fly
Explanation: Since, we are creating the object of derived class and assigning it to derived class reference itself, it will call derived method.
If we use derived object in base reference then also the derived class method will be called e.g. Bird b = new Parrot();


Q)What is output of C# code example?

class CreditCard
{
    private CreditCard() { }
   
    public void type(){
       
        Console.Write("Credit Card");
    }    
}

class Basic : CreditCard
{
    public Basic() { }
    public void Type()
    {
        Console.Write("Basic credit cards");
    }
} 

class Program
{
    static void Main(string[] args)
    {
        Basic b = new Basic();
        b.Type();
    }
}

  1. compiler error
  2. Credit Card
  3. Credit Card Basic credit cards
  4. Basic credit cards

Answer: A
This C# code flashes compile time error as we know that when we create a derived class object then first base class constructor and then derived class constructor get called. But, since, base class constructor is private, it will not be accessible to derived class resulting compilation error. You can read order of execution of constructors in c# program.


Related Posts