C# Interface – Programs for Practice

Q) What is output of C# code?

interface IAccount
{
    string AccountInfo();   
    void process();
}

class SavingAccount : IAccount
{

    public string AccountInfo()
    {
        return "Saving Account";
    }
    public void process()
    {
        Console.WriteLine("Process saving account");
    }
}

class Program
{
    static void Main()
    {
        SavingAccount account = new SavingAccount();
        Console.WriteLine(account.AccountInfo());
        account.process();        
    }
}

Output:
Saving Account
Process saving account
Explanation: Saving Account class inherits an interface and implement its methods in the class. Note that if a class inherits an interface in C# programming, then class must implement all the methods and properties of interface or interfaces if we inherit multiple interfaces. You can read C# interface with example and how to use multiple interfaces in C# program.


Q) What is output of below C# program example?

interface ITransaction
{
    void doTransaction();
}
class ConnectDatabase
{
    public void connect()
    {
        Console.WriteLine("Connect database");
    }
}

class Transaction : ConnectDatabase, ITransaction
{
    public void doTransaction()
    {
        Console.WriteLine("processing transaction");
    }    
}


class Program
{
    static void Main(string[] args)
    {
        Transaction t = new Transaction();
        t.connect();
        t.doTransaction();

    }
}

Output:

  1. Compiler error as C# multiple inheritance is not supported.
  2. Connect database, processing transaction

Answer: B
Program is absolutely correct, in C# object oriented programming, we can inherit one class and multiple interfaces.So, Program will output correctly as per methods call. You can see more about multiple interface inheritance in C Sharp with example and explanation.


Q) What is output of below C# code?

interface IFlyable
{
    void fly();
}
interface IEatable
{
    void eat();
}

class Parrot : IFlyable, IEatable
{

    public void eat()
    {
        Console.WriteLine("Parrot eating");
    }
    //Implement method of interface
    public void fly()
    {
        Console.WriteLine("Parrot flying");
    }
    
}

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

Output

  1. Compiler error, as the Parrot class have inherited two interfaces.
  2. Parrot flying. because, the parrot class can consider the first interface only, in the declaration “class Parrot: IFlyable, IEatable”.
  3. Parrot eating, parrot flying

Output: C.
Explanation: It is true that multiple inheritance is not supported in C# language, but that is true for class only. Means, multiple classes cannot be inherited. But C# language supports multiple inheritance for interfaces.


Q)What is output of the C# code?

interface I1st
{

    void rank();

}

interface I2nd
{

    void rank();

}

class Position :I1st, I2nd
{

    void rank()
    {
        Console.WriteLine("Rank 1st");
    }

    void rank()
    {
        Console.WriteLine("Rank 2nd");
    }

}


class Program
{
    static void Main(string[] args)
    {
        I1st p = new Position();
        p.rank();        
    }
}

Answer: Compiler Error.
There would be ambiguity that is which function to call from the class. If two interfaces have same method then in class implement needs an interface identifier. Here is the correct implementation of interfaces in the class.
If we use below class then output will be “Rank 1st”. Read in detail how to implement two interfaces if they have same method name.

class Position :I1st, I2nd
{

    void I1st.rank()
    {
        Console.WriteLine("Rank 1st");
    }

    void I2nd.rank()
    {
        Console.WriteLine("Rank 2nd");
    }

}

Related Posts