C#.Net find output programs (Interface) | set 2

Find the output of C#.Net programs | Interface | Set 2: Enhance the knowledge of C#.Net Interface concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 08, 2021

Question 1:

using System;

namespace Demo
{
    interface IMath
    {
        void Add(int a, int b);
        void Sub(int a, int b);
    }

    class Math : IMath
    {
        public void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
        public void Sub(int a, int b)
        {
            Console.WriteLine("Subtraction: " + (a-b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            IMath M = new Math();

            M.Add(10,20);
	     M.Sub(30, 20);
        }
    }
}

Output:

Addition: 30
Subtraction: 10
Press any key to continue . . .

Explanation:

In the above program, we created an interface IMath that contains the declaration of Add() method and Sub() method then we inherited IMath interface in the Math class and implemented both methods in Math class.

Let's look to the Main() method, here I created an object of Math class using interface IMath and then called Add() and Sub() method.

Question 2:

using System;

namespace Demo
{
    private interface IMath
    {
        void Add(int a, int b);
        void Sub(int a, int b);
    }

    class Math : IMath
    {
        public void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
        public void Sub(int a, int b)
        {
            Console.WriteLine("Subtraction: " + (a-b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            IMath M = new Math();

            M.Add(10,20);
            M.Sub(30, 20);
        }
    }
}

Output:

main.cs(5,23): error CS1527: Namespace elements cannot be explicitly 
declared as private, protected or protected internal

Explanation:

The above program will generate syntax error because we cannot define a private interface.

Question 3:

using System;

namespace Demo
{
    interface IMath
    {
        void Add(int a, int b);
        void Sub(int a, int b);
    }

    class Math1 : IMath
    {
        public void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
    }

    class Math2 : IMath
    {
        public void Sub(int a, int b)
        {
            Console.WriteLine("Subtraction: " + (a - b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Math1 M1 = new Math1();
            Math1 M2 = new Math1();

            M1.Add(10,20);
            M2.Sub(30, 20);
        }
    }
}

Output:

main.cs(11,11): error CS0535: `Demo.Math1' does not implement interface member `Demo.IMath.Sub(int, int)'
main.cs(8,14): (Location of the symbol related to previous error)
main.cs(19,11): error CS0535: `Demo.Math2' does not implement interface member `Demo.IMath.Add(int, int)'
main.cs(7,14): (Location of the symbol related to previous error)
main.cs(36,16): error CS1061: Type `Demo.Math1' does not contain a definition for `Sub' and no extension method `Sub' of type `Demo.Math1' could be found. Are you missing an assembly reference?
main.cs(11,11): (Location of the symbol related to previous error)

Explanation:

The above program will generate syntax errors because we inherited interface in two classes Math1 and Math2. But we did not implement Sub() method in Math1 class and did not implement Add() method in Math2 class.

Question 4:

using System;

namespace Demo
{
    interface IMath1
    {
        void Add(int a, int b);
    }

    interface IMath2
    {
        void Sub(int a, int b);
    }

    class Math : IMath1,IMath2
    {
        public void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
        public void Sub(int a, int b)
        {
            Console.WriteLine("Subtraction: " + (a - b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Math M = new Math();

            M.Add(10,20);
            M.Sub(30, 20);
        }
    }
}

Output:

Addition: 30
Subtraction: 10
Press any key to continue . . .

Explanation:

In the above program, we created two interfaces IMath1 and IMath2. Then we inherited both interfaces in Math class, here we implemented Add() and Sub() method in the Math class.

Now come to the Main() method, here we created an object of Math class and then called Add() and Sub() method that will print addition and subtraction of given arguments respectively.

Question 5:

using System;
namespace Demo
{
    interface IMath1
    {
        void Add(int a, int b);
    }

    interface IMath2:IMath1
    {
        void Sub(int a, int b);
    }

    class Math : IMath2
    {
        public void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
        public void Sub(int a, int b)
        {
            Console.WriteLine("Subtraction: " + (a - b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Math M = new Math();

            M.Add(10,20);
            M.Sub(30, 20);
        }
    }
}

Output:

Addition: 30
Subtraction: 10
Press any key to continue . . .

Explanation:

In the above program, we created two interfaces IMath1 and IMath2. Then we inherited the IMath1 interface in the IMath2 interface then IMath2 inherited in the Math class, here we implemented Add() and Sub() method in the Math class.

Now come to the Main() method, here we created an object of Math class and then called Add() and Sub() method that will print addition and subtraction of given arguments respectively.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.