ADVERTISEMENT
ADVERTISEMENT

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

Find the output of C#.Net programs | Method Overriding | Set 3: 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
{
    abstract class Parent
    {
        public abstract void Print();

        public abstract 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:

main.cs(9,30): error CS0500: `Demo.Parent.PrintHello()' 
cannot declare a body because it is marked abstract

Explanation:

The above program will generate compile-time errors. Because we cannot define the abstract method in the base class, we can only declare an abstract method, and then that can be overridden in the derived class.

Question 2:

using System;

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

    abstract 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(23,24): error CS0144: Cannot create an instance of the abstract class or interface `Demo.Child'
main.cs(10,20): (Location of the symbol related to previous error)

Explanation:

The above program will generate syntax error because we cannot create the object of an abstract class.

Question 3:

using System;

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

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

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

Output:

main.cs(7,37): error CS0112: A static member `Demo.Parent.Print()' cannot be marked as override, virtual or abstract
main.cs(12,37): error CS0112: A static member `Demo.Child.Print()' cannot be marked as override, virtual or abstract

Explanation:

The above program will generate syntax errors because Print() is a static method, but the static method cannot be marked as an override, virtual, or abstract.

Question 4:

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)
        {
            new Child().Print();
        }
    }
}

Output:

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

Explanation:

In the above program, we created three classes Parent, Child, and Program. Here, Program is an abstract class that contains an abstract method Print(). Then we inherited Parent class into the Child class and override the Print() method.

Now look to the Main() method,

new Child().Print();

Here, we called the Print() method using the anonymous object of child class then the message "Child class Method called" will be printed on the console screen.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.