Q:

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

0

Find the output of C#.Net programs | Method Overriding | Set 2: Enhance the knowledge of C#.Net Method Overriding concepts by solving and finding the output of some C#.Net programs.

Question 1:

using System;

namespace Demo
{
    class Parent
    {
        public abstract void Print();
    }

    class Child:Parent
    {
        public override void Print()
        {
            Console.WriteLine("Child class Method called");
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Parent P = new Child();
            P.Print();
        }
    }
}

Question 2:

using System;

namespace Demo
{
    abstract class Parent
    {
        public abstract void Print();
    }

    class Child:Parent
    {
        public override void Print()
        {
            Console.WriteLine("Child class Method called");
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Parent P = new Child();
            P.Print();
        }
    }
}

Question 3:

using System;

namespace Demo
{
    abstract class Parent
    {
        public abstract void Print();
    }

    class Child:Parent
    {
        public override void Print()
        {
            Console.WriteLine("Child class Method called");
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Parent P = new Parent();
            P.Print();
        }
    }
}

Question 4:

using System;

namespace Demo
{
    abstract class Parent
    {
        public virtual void Print();
    }

    class Child:Parent
    {
        public override void Print()
        {
            Console.WriteLine("Child class Method called");
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Parent P = new Child();
            P.Print();
        }
    }
}

Question 5:

using System;

namespace Demo
{
    abstract class Parent
    {
        public abstract void Print();

        public void PrintHello()
        {
            Console.WriteLine("Hello World");
        }
    }

    class Child:Parent
    {
        public override void Print()
        {
            Console.WriteLine("Child class Method called");
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Parent P = new Child();
            P.Print();
            P.PrintHello();
        }
    }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Output:

main.cs(7,30): error CS0513: `Demo.Parent.Print()' is abstract 
but it is declared in the non-abstract class `Demo.Parent'
main.cs(5,11): (Location of the symbol related to previous error)

Explanation:

The above program will generate syntax error because we cannot create an abstract method in the non-abstract class.

Answer 2:

Output:

Child class Method called
Press any key to continue . . .

Explanation:

In the above program, we created an abstract class Parent that contains the declaration of the Print() method and then we override the Print() method inside the Child class. The Parent class is inherited in Child class.

Now look to the Main() method,

static void Main(string[] args)
{
    Parent P = new Child();
    P.Print();
}

In the above method we created object P using Child() class constructor, we override the Print() method in Child class that's why Child class constructor is called using object P.

Answer 3:

Output:

main.cs(23,24): error CS0144: Cannot create an instance of the 
abstract class or interface `Demo.Parent'
main.cs(5,20): (Location of the symbol related to previous error)

Explanation:

The above program will generate syntax error because here create the object of Parent class. But Parent is an abstract class and we cannot create the object of a Parent class.

Answer 4:

Output:

main.cs(7,29): error CS0501: `Demo.Parent.Print()' must have a body 
because it is not marked abstract, extern, or partial

Explanation:

The above program will generate syntax error because we declared the Print() method in the Parent class without using the abstract keyword. Here Parent is an abstract class, so here we need to use the abstract keyword instead of the virtual keyword.

Answer 5:

Output:

Child class Method called
Hello World
Press any key to continue . . .

Explanation:

In the above program, we created three classes Parent, Child, and Program. Parent is an abstract class that contains two methods Print() and PrintHello(). Here, Print() is an abstract method and PrintHello() is a non-abstract method. The Print() method is overridden in the Child class.

Let's look to the Main() method, here we created object P then call Print() and PrintHello() method.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now