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

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.
Submitted by Nidhi, on February 09, 2021

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();
        }
    }
}

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.

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();
        }
    }
}

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.

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();
        }
    }
}

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.

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();
        }
    }
}

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.

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();
        }
    }
}

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.





Comments and Discussions!

Load comments ↻





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