Q:

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

belongs to collection: C#.Net find output programs

0

Question 1:

using System;

namespace Demo
{
    class ABC
    {
        public ABC()
        {
            Console.WriteLine("ABC-Constructor called");
        }
        public void FUN()
        {
            Console.WriteLine("FUN() called");
        }
    }

    class XYZ : ABC
    { 
        public XYZ()
        {
            Console.WriteLine("XYZ-Construtor called");
        }
    }

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

            X.FUN();
        }
    }
}

Question 2:

using System;

namespace Demo
{
    class ABC
    {
        public ABC()
        {
            Console.WriteLine("ABC-Constructor called");
        }
        protected void FUN()
        {
            Console.WriteLine("FUN() called");
        }
    }

    class XYZ : ABC
    { 
        public XYZ()
        {
            Console.WriteLine("XYZ-Construtor called");
        }
    }

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

            X.FUN();
        }
    }
}

Question 3:

using System;

namespace Demo
{
    class ABC
    {
        protected ABC()
        {
            Console.WriteLine("ABC-Constructor called");
        }
        public void FUN()
        {
            Console.WriteLine("FUN() called");
        }
    }

    class XYZ : ABC
    { 
        public XYZ()
        {
            Console.WriteLine("XYZ-Construtor called");
        }
    }

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

            X.FUN();
        }
    }
}

Question 4:

using System;

namespace Demo
{
    class ABC
    {
        protected ABC()
        {
            Console.WriteLine("ABC-Constructor called");
        }
        public void FUN()
        {
            Console.WriteLine("FUN() called");
        }
    }

    class XYZ : ABC
    { 
        protected XYZ()
        {
            Console.WriteLine("XYZ-Construtor called");
        }
    }

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

            X.FUN();
        }
    }
}

Question 5:

using System;

namespace Demo
{
    class ABC
    {
        public ~ABC()
        {
            Console.WriteLine("ABC-Destructor called");
        }
        public void FUN()
        {
            Console.WriteLine("FUN() called");
        }
    }

    class XYZ : ABC
    { 
        public ~XYZ()
        {
            Console.WriteLine("XYZ-Destrutor called");
        }
    }

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

            X.FUN();
        }
    }
}

All Answers

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

Answer 1 :

Output:

ABC-Constructor called
XYZ-Construtor called
FUN() called
Press any key to continue . . .

Explanation:

In the above program, we created two classes ABC and XYZ. Here, we inherited class ABC into class XYZ.

Class ABC contains a no-argument constructor and a method FUN(), and class XYZ contains a no-argument constructor.

Now look to the Main() method, the Main() method of Program class is the entry point of the program. Here, we created the object of class XYZ then it calls the constructor of class ABC before calling the constructor of class XYZ because of inheritance. Then we called the FUN() method of ABC class.

Answer 2:

Output:

main.cs(32,15): error CS1540: Cannot access protected member `Demo.ABC.FUN()' via a qualifier of type `Demo.XYZ'. The qualifier must be of type `Demo.Program' or derived from it
main.cs(11,24): (Location of the symbol related to previous error)
main.cs(32,15): error CS0122: `Demo.ABC.FUN()' is inaccessible due to its protection level
main.cs(11,24): (Location of the symbol related to previous error)

Explanation:

The above program will generate a syntax error because a protected member of a class can only be accessed in the child or derived class, here FUN() method is called in Main() method of Program, which is not possible due to protection level.

Answer 3:

Output:

ABC-Constructor called
XYZ-Construtor called
FUN() called
Press any key to continue . . .

Explanation:

In the above program, we created two classes ABC and XYZ. Here, we inherited class ABC into class XYZ.

Class ABC contains a no-argument constructor with a protected access modifier and a method FUN(), and class XYZ contains a no-argument constructor.

Now look to the Main() method, the Main() method of Program class is the entry point of the program. Here, we created the object of class XYZ then it calls the constructor of class ABC before calling the constructor of class XYZ because of inheritance. Here, constructor of ABC class is protected member; here it was called from child class XYZ. Then we called the FUN() method of ABC class.

Answer 4:

Output:

main.cs(30,21): error CS1540: Cannot access protected member `Demo.XYZ.XYZ()' via a qualifier of type `Demo.XYZ'. The qualifier must be of type `Demo.Program' or derived from it
main.cs(19,19): (Location of the symbol related to previous error)
main.cs(30,21): error CS0122: `Demo.XYZ.XYZ()' is inaccessible due to its protection level
main.cs(19,19): (Location of the symbol related to previous error)

Explanation:

The above program will generate a syntax error because here we accessed the constructor of XYZ class from Program class, but here we declared constructor of XYZ as a protected member. But protected members can only be accessed in the same class on the derived class.

Answer 5:

Output:

main.cs(7,17): error CS0106: The modifier `public' is not valid for this item
main.cs(19,17): error CS0106: The modifier `public' is not valid for this item

Explanation:

The above program will generate two syntax errors because we cannot use an access modifier with destructors.

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 (Inheritance) | set 2... >>
<< C#.Net find output programs (Constructors & Destru...