Q:

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

belongs to collection: C#.Net find output programs

0

Find the output of C#.Net programs | Method Overriding | Set 1: 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 void Print()
        {
            Console.WriteLine("Parent class Method called");
        }
    }

    class Child:Parent
    {
        public 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();

            P = new Child();
            P.Print();
        }
    }
}

Question 2:

using System;

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

    class Child:Parent
    {
        public virtual 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();

            P = new Child();
            P.Print();
        }
    }
}

Question 3:

using System;

namespace Demo
{
    class Parent
    {
        public virtual void Print()
        {
            Console.WriteLine("Parent class Method called");
        }
    }

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

            P = new Child();
            P.Print();
        }
    }
}

Question 4:

using System;
namespace Demo
{
    class Parent
    {
        public void Print()
        {
            Console.WriteLine("Parent class Method called");
        }
    }

    class Child:Parent
    {
        public new 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();

            P = new Child();
            P.Print();
        }
    }
}

Question 5:

using System;

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

All Answers

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

Answer 1:

Output:

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

Explanation:

In the above program, we created three classes Parent, Child, and Program. Here, Parent class inherited in Child class. Both Parent and Child class contains the Print() method.

Now look to the Main() method of Program class, Here we created the object P of Parent class and then call Print() method, then it will call the Print() method of Parent class,

P = new Child();

Using the above statement we re-initialized the P using Child class. Then we called the Print() method, it will call the Print() method of Parent class again. If we want to override the method in Child class then we need to use virtual and override keywords.

Answer 2:

Output:

main.cs(7,30): error CS0115: `Demo.Parent.Print()' is marked as 
an override but no suitable method found to override

Explanation:

The above program will generate syntax error because we used the override keyword in the Parent class method. We can use the override method in child class only. Because we can override existing parent class method in child class.

Answer 3:

Output:

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

Explanation:

In the above program, we created three classes Parent, Child, and Program. Here, Parent class inherited in Child class. Both Parent and Child class contains the Print() method. We used the virtual keyword in the Parent class method and override keyword in Child class.

Now look to the Main() method of Program class, Here we created the object P of Parent class and then call Print() method, then it will call the Print() method of Parent class.

P = new Child();

Using the above statement we re-initialized the P using Child class. Then we called the Print() method, it will call the Print() method of Parent class again. If we want to override the method in Child class then we need to use virtual and override keywords.

Answer 4:

Output:

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

Explanation:

In the above program, we created three classes Parent, Child, and Program. Here, Parent class inherited in Child class. Both Parent and Child class contains the Print() method.

Now look to the Main() method of Program class, Here, we created the object P of Parent class and then call Print() method, then it will call the Print() method of Parent class.

P = new Child();

Using the above statement we re-initialized the P using Child class. Then we called the Print() method, it will call the Print() method of Parent class again. If we want to override the method in Child class then we need to use virtual and override keywords.

Answer 5:

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 did not define the Print() method in Parent class. That's why errors will be generated in the above program.

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

total answers (1)

C#.Net find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C#.Net find output programs (Method Overriding) | ... >>
<< C#.Net find output programs (Method Overloading) |...